I have a parent-child json based data
[ { "_id":"624015bb69f627d1d3484b06", "name":"11111", "created":"2022-03-27T07:36:50.492Z", "children":[ { "_id":"624015c369f627d1d3484b0b", "name":"222222", "created":"2022-03-27T07:36:50.492Z", "children":[ ], "parent":"624015bb69f627d1d3484b06", "path":"624015bb69f627d1d3484b06,624015c369f627d1d3484b0b", "__v":0 }, { "_id":"624015d069f627d1d3484b10", "name":"333333", "created":"2022-03-27T07:36:50.492Z", "children":[ { "_id":"6240161269f627d1d3484b15", "name":"444444", "created":"2022-03-27T07:36:50.492Z", "children":[ { "_id":"624021be69f627d1d3484b1f", "name":"5555555", "created":"2022-03-27T07:36:50.492Z", "children":[ { "_id":"624021cc69f627d1d3484b24", "name":"555551111", "created":"2022-03-27T07:36:50.492Z", "children":[ ], "parent":"624021be69f627d1d3484b1f", "path":"624015bb69f627d1d3484b06,624015d069f627d1d3484b10,6240161269f627d1d3484b15,624021be69f627d1d3484b1f,624021cc69f627d1d3484b24", "__v":0 }, { "_id":"624021d569f627d1d3484b29", "name":"555552222", "created":"2022-03-27T07:36:50.492Z", "children":[ ], "parent":"624021be69f627d1d3484b1f", "path":"624015bb69f627d1d3484b06,624015d069f627d1d3484b10,6240161269f627d1d3484b15,624021be69f627d1d3484b1f,624021d569f627d1d3484b29", "__v":0 }, { "_id":"624021de69f627d1d3484b2e", "name":"555553333", "created":"2022-03-27T07:36:50.492Z", "children":[ ], "parent":"624021be69f627d1d3484b1f", "path":"624015bb69f627d1d3484b06,624015d069f627d1d3484b10,6240161269f627d1d3484b15,624021be69f627d1d3484b1f,624021de69f627d1d3484b2e", "__v":0 }, { "_id":"624021eb69f627d1d3484b33", "name":"555554444", "created":"2022-03-27T07:36:50.492Z", "children":[ ], "parent":"624021be69f627d1d3484b1f", "path":"624015bb69f627d1d3484b06,624015d069f627d1d3484b10,6240161269f627d1d3484b15,624021be69f627d1d3484b1f,624021eb69f627d1d3484b33", "__v":0 }, { "_id":"624021f469f627d1d3484b38", "name":"555555555", "created":"2022-03-27T07:36:50.492Z", "children":[ ], "parent":"624021be69f627d1d3484b1f", "path":"624015bb69f627d1d3484b06,624015d069f627d1d3484b10,6240161269f627d1d3484b15,624021be69f627d1d3484b1f,624021f469f627d1d3484b38", "__v":0 } ], "parent":"6240161269f627d1d3484b15", "path":"624015bb69f627d1d3484b06,624015d069f627d1d3484b10,6240161269f627d1d3484b15,624021be69f627d1d3484b1f", "__v":0 } ], "parent":"624015d069f627d1d3484b10", "path":"624015bb69f627d1d3484b06,624015d069f627d1d3484b10,6240161269f627d1d3484b15", "__v":0 } ], "parent":"624015bb69f627d1d3484b06", "path":"624015bb69f627d1d3484b06,624015d069f627d1d3484b10", "__v":0 } ], "parent":null, "path":"624015bb69f627d1d3484b06", "__v":0 } ]
i want to search the end leaf node by searching its _id value, for example I want to find the leaf node whose id is ‘624021f469f627d1d3484b38’, it should output the whole node value whose name is ‘55555555’,
I have tried the following methods.
const find = (array, id) => { var result; array.some(o => result = o._id === id ? o : find(o.children || [], id)); return result; }; const findChildById = (arr, id) => { const result = arr.find(o => o._id === id) if (result) return result for (const cm of arr) { const result = cm.children.find(o => o._id === id) if (result) return result } } let node = find( nodeData, '624021f469f627d1d3484b38'); let node = findChildById( nodeData, '624021f469f627d1d3484b38');
but both methods output the undefined,
Advertisement
Answer
const findChildren = (arrElement, matchingID) => { if (arrElement._id === matchingID) { return arrElement; } else if (arrElement.children.length) { let children = null; arrElement.children.forEach(element => { children = findChildren(element, matchingID); }); return children; } return null; }; console.log(findChildren(arrayChildren[0], '624021f469f627d1d3484b38'));