我有以下收藏品。每个对象可能有一个子对象数组,可能有一个子对象数组等等...我想迭代这个集合并检索对象的属性与特定值匹配。
[{
"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": []
}]
}]
}]
}]
}]
我想用value = 47来检索对象; 我想在loadash中使用它。因为递归函数占用过多的内存。
答案 0 :(得分:0)
我知道你想在loadash中使用它,但是如果你无法得到答案,这里有一个supercoolrecursive函数,可以完成你所需要的而不会消耗内存。
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))
&#13;