I am dealing with a JSON API response that has 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: [...]
},
...
]
In addition, I have a small array of content IDs arr2 = ['id2','id3']
. My task is to search the API response data to find any content_id that matches an ID in arr2
.
Although I have some lodash code in place, I realize that my nested forEach approach may not be the most efficient:
_.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
}
})
})
})
})
Do you have any suggestions on how I could optimize this code?