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 中,但如果你得不到答案,这里有一个超级酷的递归函数,它可以完成你需要的事情,而根本不会消耗内存。

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))