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
-
You should probably mention the desired output so that people can help and see what you are doing wrong.Anurag Singh Bisht– Anurag Singh Bisht2017年08月08日 11:31:17 +00:00Commented Aug 8, 2017 at 11:31
-
You repeat the object looping within it by perform some 'if'Muhaimin– Muhaimin2017年08月08日 11:32:09 +00:00Commented Aug 8, 2017 at 11:32
-
if you run the code, you can actually see what I'm talking about. but I will edit the question. thanks @AnuragSinghBishtJonathan– Jonathan2017年08月08日 11:32:42 +00:00Commented Aug 8, 2017 at 11:32
-
Beautify the JSON object maybe,Deepak Kamat– Deepak Kamat2017年08月08日 11:37:17 +00:00Commented Aug 8, 2017 at 11:37
-
Why the last item is not an array of objects?a45b– a45b2017年08月08日 11:41:11 +00:00Commented Aug 8, 2017 at 11:41
2 Answers 2
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);
}
2 Comments
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);
}
});
8 Comments
for -in is not useful for this purpose