简体   繁体   中英

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_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 Machine
-itemno batchno undefined
-itemno itemno undefined

How can I loop through everything and still get all the values from the cost_center which doesn't contain an array ? Thanks

Instead of using for-in to loop through the array, you can use forEach property.

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. If length exists than it's an array and you can loop to get the answer. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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