如何通过迭代内生成的字符串访问JSON对象索引

时间:2017-03-06 20:57:43

标签: javascript json

我有以下JSON对象:

value = {
"montoBase01": 10,
"montoBase02": 5,
"montoBase03": 34,
"montoBase04": 5,
"montoBase05": 25,
"montoBase06": 134,
"montoBase07": 25,
"montoBase08": 345,
"montoBase09": 23,
"montoBase10": 45,
"montoBase11": 234,
"montoBase12": 5
};

我想使用像例如

这样的循环函数来检索每个索引
for(i=1;i<=12;i++){
   var index = ""; 
   if(i<10)
      index = "montoBase0"+i;
   else
      index = "montoBase"+i;
   var mb = value.index
}

但我总是得到未定义的值,是否可以动态检索索引?如果是的话,我的代码出了什么问题?

3 个答案:

答案 0 :(得分:2)

您可以使用Object.entries()来迭代对象的属性和值

&#13;
&#13;
let value = {
  "montoBase01": 10,
  "montoBase02": 5,
  "montoBase03": 34,
  "montoBase04": 5,
  "montoBase05": 25,
  "montoBase06": 134,
  "montoBase07": 25,
  "montoBase08": 345,
  "montoBase09": 23,
  "montoBase10": 45,
  "montoBase11": 234,
  "montoBase12": 5
};

for (let [key, val] of Object.entries(value)) {
  // do stuff with `key`, `val`
  console.log(key, val);
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您必须使用方括号

// Code goes here

var items = {
    "TransactionLine": [
      {
        "Product": {
          "Id": null,
          "Codes": [
            "1112"
          ],
          "Sku": null
        },
        "TotalValue": 2.35,
      },
      {
        "Product": {
          "Id": null,
          "Codes": [
            "1113"
          ],
          "Sku": null
        },
        "TotalValue": 2.15,
      }
    ],
    "CustomData": {}
  };
  
  var itemPrice = [];
  for (var i = 0; i < items.TransactionLine.length; i++) {
    var el = items.TransactionLine[i];
    itemPrice.push(el.TotalValue);
    console.log(el.TotalValue);
  }
  
  var newJson = {
    "OrderLines": [
          {
            "Product": {
              "Id": 9,
              "Codes": [
                "1113"
              ],
              "Sku": "CS1113"
            },
            "TotalOriginalValue": 0, // asign the price here
          },
          {
            "Product": {
              "Id": 21,
              "Codes": [
                "1112"
              ],
              "Sku": "CS1112"
            },
            "TotalOriginalValue": 0, // asign the price here
          }
        ]
  };
  
  var newPrice = [];
  for (var x = 0; x < newJson.OrderLines.length; x++) {
    var xd = newJson.OrderLines[x].TotalOriginalValue;
    xd = itemPrice[x];
    newjson = {
      "TotalOriginalValue": xd
    };
    newPrice.push(newjson);
  }

  console.log('newJSON >> ', newPrice);

或:

$.formatField(value["montoBase0"+i])
$.formatField(value["montoBase"+i])

阅读Property accessor

请注意: index = "montoBase0"+i; var mb = value[index] var是一个对象,而不是JSON:)

答案 2 :(得分:0)

尝试使用jQuery .each()

的内置函数

$(document).ready(function(){

value = {
"montoBase01": 10,
"montoBase02": 5,
"montoBase03": 34,
"montoBase04": 5,
"montoBase05": 25,
"montoBase06": 134,
"montoBase07": 25,
"montoBase08": 345,
"montoBase09": 23,
"montoBase10": 45,
"montoBase11": 234,
"montoBase12": 5
};

$.each(value, function(key, value){
if(value < 10)
  console.log("montoBase0"+value);
else
  console.log("montoBase"+value)
})

})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>