简体   繁体   English

使用JavaScript遍历对象数组

[英]Loop through object array with javascript

I'm trying to loop through the following : 我正在尝试遍历以下内容:

{
    "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); } } } } 

I have found similar question here but the difference lay on my data, my first 2 cost_centers have arrays as elements, my the 3rd one isn't an array. 我在这里发现了类似的问题但是区别在于我的数据,我的前两个cost_centers具有数组作为元素,而我的第三个不是数组。

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

How can I loop through everything and still get all the values from the cost_center which doesn't contain an array ? 我如何遍历所有内容并仍然从不包含数组的cost_center中获取所有值? Thanks 谢谢

Instead of using for-in to loop through the array, you can use forEach property. 您可以使用forEach属性,而不是使用for-in遍历数组。

Also, if your last element does not contains an array, I am assuming it will always be an object. 另外,如果您的最后一个元素不包含数组,那么我假设它将始终是一个对象。 So to test that, you can check for length of array. 因此,要进行测试,您可以检查数组的length If length exists than it's an array and you can loop to get the answer. 如果length存在,则为数组,您可以循环获取答案。 If not then it's an object, so you can directly print the values. 如果不是,那么它是一个对象,因此您可以直接打印值。

 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); } }); 

No need for hasOwnProperty , and item.constructor === Array can be used to check: 不需要hasOwnProperty ,并且item.constructor === Array可用于检查:

 var 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); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM