I have the following collection. Each object may have a children array of objects, which may have a children array of objects and so on... I want to iterate over this collection and retrieve the object with its property matching a particular value.
[{
"value": 39,
"label": "Bangalore",
"path": "fa fa-tachometer",
"parentmenu": null,
"layout": null,
"children": [{
"value": 40,
"label": "Building1",
"path": "fa fa-tachometer",
"parentmenu": 39,
"layout": null,
"children": [{
"value": 41,
"label": "Floor1",
"path": "fa fa-tachometer",
"parentmenu": 40,
"layout": null,
"children": [{
"value": 42,
"label": "Telemon_35454",
"path": "fa fa-tachometer",
"parentmenu": 41,
"layout": null,
"children": [{
"value": 43,
"label": "MSensor1",
"path": "fa fa-tachometer",
"parentmenu": 42,
"layout": null,
"children": []
}, {
"value": 44,
"label": "MSensor2",
"path": "fa fa-tachometer",
"parentmenu": 42,
"layout": null,
"children": []
}]
}]
}, {
"value": 45,
"label": "Floor3",
"path": "fa fa-tachometer",
"parentmenu": 40,
"layout": null,
"children": [{
"value": 46,
"label": "Telemon_35454",
"path": "fa fa-tachometer",
"parentmenu": 45,
"layout": null,
"children": [{
"value": 47,
"label": "Battery",
"path": "fa fa-tachometer",
"parentmenu": 46,
"layout": null,
"children": []
}]
}]
}]
}]
}]
I want to retrive the object with value = 47; I want it in loadash. because recursive function takes too mush memory.
I know you want it in loadash, but if you cant get an answer, here is a supercoolrecursivefunction that does what you need without consuming memory at all.
var arraylong = [{
"value": 39,
"label": "Bangalore",
"path": "fa fa-tachometer",
"parentmenu": null,
"layout": null,
"children": [{
"value": 40,
"label": "Building1",
"path": "fa fa-tachometer",
"parentmenu": 39,
"layout": null,
"children": [{
"value": 41,
"label": "Floor1",
"path": "fa fa-tachometer",
"parentmenu": 40,
"layout": null,
"children": [{
"value": 42,
"label": "Telemon_35454",
"path": "fa fa-tachometer",
"parentmenu": 41,
"layout": null,
"children": [{
"value": 43,
"label": "MSensor1",
"path": "fa fa-tachometer",
"parentmenu": 42,
"layout": null,
"children": []
}, {
"value": 44,
"label": "MSensor2",
"path": "fa fa-tachometer",
"parentmenu": 42,
"layout": null,
"children": []
}]
}]
}, {
"value": 45,
"label": "Floor3",
"path": "fa fa-tachometer",
"parentmenu": 40,
"layout": null,
"children": [{
"value": 46,
"label": "Telemon_35454",
"path": "fa fa-tachometer",
"parentmenu": 45,
"layout": null,
"children": [{
"value": 47,
"label": "Battery",
"path": "fa fa-tachometer",
"parentmenu": 46,
"layout": null,
"children": []
}]
}]
}]
}]
}]
function superRecursiveFunctionExtraLargeForExamplePurposesHiMom(array, id){
for(var k = 0; k < array.length; k++){
if(array[k].value == id){
return array[k]
}
let response = superRecursiveFunctionExtraLargeForExamplePurposesHiMom(array[k].children, id)
if(response != undefined)
return response;
}
return undefined;
}
console.log(superRecursiveFunctionExtraLargeForExamplePurposesHiMom(arraylong, 46))