I have a JSON API response with the following structure
[
{
title: "top1",
sections: [
{
section_title: "section1",
content: [
{
content_title: "title1",
content_id: "id1"
},
{
content_title: "title2",
content_id: "id2"
}
]
},
{
section_title: "section2",
content: [
{
content_title: "title3",
content_id: "id3"
},
{
content_title: "title4",
content_id: "id4"
}
]
}
]
}, {
title: "top2",
sections: [...]
},
...
]
I also have a small array of content IDs arr2 = ['id2','id3']
. I need to search the data from the API request to find any content_id that is contained in arr2
.
I have some working lodash code, but my approach of nested forEach does not seem to be the most efficient approach:
_.forEach(response, function(top) {
_.forEach(top.sections, function(section) {
_.forEach(section.content, function(content) {
_.forEach(arr2, function(id) {
if(id === content.content_id) {
// Do stuff
}
})
})
})
})
How could I improve this code?
content
object, a property to true on the parentsection
object and a property to true on the grandparenttop
object:content.owned = true
,section.owned = true
,top.owned = true