![](/img/trans.png)
[英]Convert Javascript object and children into array of objects - lodash
[英]LoDash find the object whose property matches the value from array of objects with children array
我有以下收藏。 每個對象都可能具有對象的子對象數組,其中可能具有對象的子對象數組,依此類推...我想遍歷此集合並檢索其屬性與特定值匹配的對象。
[{
"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": []
}]
}]
}]
}]
}]
我想檢索值為47的對象; 我想要它在loadash中。 因為遞歸函數占用了過多的內存。
我知道您需要在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))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.