使用javascript

时间:2017-08-08 11:27:47

标签: javascript arrays json object

我试图循环使用以下内容:

{
    "machine": [{
        "cost_center": "15023 DC1 M3 - Hassia1",
        "item": [{
            "batchno": "367721",
            "itemno": "12028"
        }, {
            "batchno": "367722",
            "itemno": "12328"
        }, {
            "batchno": "367723",
            "itemno": "12608"
        }]
    }, {
        "cost_center": "15033 DC1 M4 - Hamba",
        "item": [{
            "batchno": "367729",
            "itemno": "11850"
        }, {
            "batchno": "367730",
            "itemno": "11851"
        }, {
            "batchno": "367731",
            "itemno": "11852"
        }]
    }, {
        "cost_center": "15043 DC1 M5 - 1KG Machine",
        "item": {
            "batchno": "367732",
            "itemno": "12592"
        }
    }]
}



var json = '{"machine":[{"cost_center":"15023 DC1 M3 - Hassia1","item":[{"batchno":"367721","itemno":"12028"},{"batchno":"367722","itemno":"12328"},{"batchno":"367723","itemno":"12608"}]},{"cost_center":"15033 DC1 M4 - Hamba","item":[{"batchno":"367729","itemno":"11850"},{"batchno":"367730","itemno":"11851"},{"batchno":"367731","itemno":"11852"}]},{"cost_center":"15043 DC1 M5 - 1KG Machine","item":{"batchno":"367732","itemno":"12592"}}]}';

var obj = JSON.parse(json);
var db = obj.machine;

for (var m in db) {
    if (db.hasOwnProperty(m)) {
        var item = db[m].item;
        console.log('cost_center ' + m + ' = ' + db[m].cost_center);
        for (var i in item) {
            if (item.hasOwnProperty(i)) {
                var prod = item[i];
                console.log('-itemno ' + i + ' ' + prod.itemno);
            }
        }
    }
}




我发现了类似的问题here,但差异在于我的数据,我的前2个cost_centers有数组作为元素,我的第3个不是数组。

cost_center 0 = 15023 DC1 M3 - Hassia1
-itemno 0 12028
-itemno 1 12328
-itemno 2 12608
cost_center 1 = 15033 DC1 M4 - Hamba
-itemno 0 11850
-itemno 1 11851
-itemno 2 11852
cost_center 2 = 15043 DC1 M5 - 1KG机器
-itemno batchno undefined
-itemno itemno undefined

如何循环遍历所有内容并仍然从cost_center中获取不包含数组的所有值? 感谢

2 个答案:

答案 0 :(得分:1)

您可以使用for-in属性,而不是使用forEach循环遍历数组。

另外,如果你的最后一个元素不包含数组,我假设它总是一个对象。因此,要测试它,您可以检查数组的length。如果length存在而不是数组,您可以循环获取答案。如果没有那么它就是一个对象,所以你可以直接打印这些值。

var json = '{"_vger_record_id":"2409247","ExtraData":"","machine":[{"_vger_record_id":"2409248","cost_center":"15023 DC1 M3 - Hassia1","ExtraData":"","item":[{"_vger_record_id":"2409249","batchno":"367721","itemno":"12028"},{"_vger_record_id":"2409250","batchno":"367722","itemno":"12328"},{"_vger_record_id":"2409251","batchno":"367723","itemno":"12608"}]},{"_vger_record_id":"2409257","cost_center":"15033 DC1 M4 - Hamba","ExtraData":"","item":[{"_vger_record_id":"2409258","batchno":"367729","itemno":"11850"},{"_vger_record_id":"2409259","batchno":"367730","itemno":"11851"},{"_vger_record_id":"2409260","batchno":"367731","itemno":"11852"}]},{"_vger_record_id":"2409261","cost_center":"15043 DC1 M5 - 1KG Machine","ExtraData":"","item":{"_vger_record_id":"2409262","batchno":"367732","itemno":"12592"}}]}';

var obj = JSON.parse(json);
var db = obj.machine;


db.forEach(function(m) {
  console.log('cost_center  = ' + m.cost_center); 
  if(m.item.length) {
    m.item.forEach(function(i) {
      console.log('-itemno = ' + i.itemno);
    });
  } else {
    console.log('-itemno = ' + m.item.itemno);
  }
  
});

答案 1 :(得分:1)

无需item.constructor === Arrayvar db = JSON.parse('{"machine":[{"cost_center":"15023 DC1 M3 - Hassia1","item":[{"batchno":"367721","itemno":"12028"},{"batchno":"367722","itemno":"12328"},{"batchno":"367723","itemno":"12608"}]},{"cost_center":"15033 DC1 M4 - Hamba","item":[{"batchno":"367729","itemno":"11850"},{"batchno":"367730","itemno":"11851"},{"batchno":"367731","itemno":"11852"}]},{"cost_center":"15043 DC1 M5 - 1KG Machine","item":{"batchno":"367732","itemno":"12592"}}]}').machine; for (var m = 0; m < db.length; m++) { var e = db[m], item = e.item; console.log('cost_center ' + m + ' = ' + e.cost_center); if (item.constructor === Array) for (var i = 0; i < item.length; i++) console.log(' itemno ' + i + ' = ' + item[i].itemno); else console.log(' itemno 0 = ' + item.itemno); }可用于检查:

&#13;
&#13;
&until=timestamp
&#13;
&#13;
&#13;