The current code works with no problem. I like to know if there is a cleaner or better way to remove objects from the root of my array if a value is found somewhere in a nested object. I want to remove the object
const baseArray = [
{
title: 'Test folder',
UUID: '54F5E250-1C5F-49CC-A963-5B7FD8884E48',
Children: [
{
title: 'nothing',
UUID: 123,
},
]
},
{
title: 'nothing',
UUID: 123,
},
]
const currArray = baseArray
const removeRootUUID = (baseArray, currArray) => {
const delInRoot = uuidRef => {
baseArray.forEach((obj, i) => {
if (obj.UUID === uuidRef) {
baseArray.splice(i, 1);
}
});
};
for (const obj of currArray) {
if (obj.Children) {
obj.Children.forEach(node => {
delInRoot(node.UUID);
});
removeRootUUID(baseArray, obj.Children);
}
}
};
output I am looking for is below. (because UUID is founded in root and in another nested object)
{
title: 'Test folder',
UUID: '54F5E250-1C5F-49CC-A963-5B7FD8884E48',
Children: [
{
title: 'nothing',
UUID: 123,
},
],
}