var categories = [{
"categories": {
"data": [{
"parent_id": 109,
},
{
"parent_id": 0,
}]
}
}];
Hello, I have the above JSON object I'm trying to access the parent_id in categories with the following:
categories.data[i].parent_id !== 0
This is using a for loop with i < categories.length.
I've google around and im not finding a good answer to this, is there a cleaner way to access the parent_id in the JSON?
EDIT: I implemented the suggested solutions with this and it's working 100% with access to the JSON however the Map() is taking them as undefined. Heres what I did:
let byParentId = new Map();
for (let item of sample) {
for (let data of item.categories.data) {
if (data.parent_id !== 0) {
if (!byParentId.has(data.parent_id)) {
byParentId.set(data.parent_id, item.categories.data);
} else {
byParentId.get(data.parent_id).push(item.categories.data);
}
}
}
}