Encuentra un objeto en una matriz de objetos profundamente anidados recursivamente
Tengo una matriz, y cada elemento es un objeto con ID únicos. Algunos artículos también pueden tener hijos, y puede tener una matriz de niños dentro de una matriz de niños. Estoy tratando de elegir un artículo usando una identificación.
const array = [
{
uuid: '40E75F3DE56B4B11B3AFBDE46785737B'
}, {
uuid: '9CEF74766BBB4B9682B7817B43CEAE48'
}, {
uuid: '34F209A883D3406FBA6BACD9E07DB1D9',
children: [{
uuid: 'F429C51BF01C405DA517616E0E16DE4E',
children: [{
uuid: '8823CFCE7D4645C68991332091C1A05C'
}, {
uuid: '58A9345E881F48C980498C7FFB68667D'
}]
}]
}, {
uuid: '152488CC33434A8C9CACBC2E06A7E535'
}, {
uuid: '9152B3DEF40F414BBBC68CACE2F5F6E4'
}, {
uuid: 'B9A39766B17E4406864D785DB6893C3D'
},
{
uuid: '3J4H4J5HN6K4344D785DBJ345HSSODF',
children: [
{
uuid: 'EAB14DD72DA24BB88B4837C9D5276859'
},
{
uuid: 'FFA80D043380481F8835859A0839512B'
},
{
uuid: '9679687190354FA79EB9D1CA7B4962B1'
}
]
}
]
El siguiente código funciona para una matriz simple sin hijos. Necesito una función o un módulo lodash que pueda buscar de forma recursiva toda la matriz y devolver el objeto (elemento en la matriz)
findContainingObject(array, uuid) {
let result = [];
result = array.filter( item => {
return item.uuid === uuid
})
return result;
}
Salidas esperadas:
findContainingObject(array, '40E75F3DE56B4B11B3AFBDE46785737B')
{
uuid: '40E75F3DE56B4B11B3AFBDE46785737B'
}
findContainingObject(array, '34F209A883D3406FBA6BACD9E07DB1D9')
{
uuid: '34F209A883D3406FBA6BACD9E07DB1D9',
children: [{
uuid: 'F429C51BF01C405DA517616E0E16DE4E',
children: [{
uuid: '8823CFCE7D4645C68991332091C1A05C'
}, {
uuid: '58A9345E881F48C980498C7FFB68667D'
}]
}]
}
findContainingObject(array, '58A9345E881F48C980498C7FFB68667D')
{
uuid: '58A9345E881F48C980498C7FFB68667D'
}