Reputation: 241
How can I remove the duplicate from the root. Basically I want to look at the full array at the UUID property. If a UUID from the root of the array is found anywhere else in the array I want to remove the object from the root.
Example: "UUID": "9E741BEC-7BC1-49C0-8214-AB5A65762CB7" is found to the top level of the tree. Its also found in one of the Children. so I want to remove the on that is at the top level
{
"UUID": "9E741BEC-7BC1-49C0-8214-AB5A65762CB7",
"Visible": false,
"WasVisibleTransient": false,
"WhichCorners": 15,
"Width": 796.5216064453125,
"Width2": 796.5216064453125,
"Width3": 796.5216064453125,
"title": "Frances Stowe Great lesson 👌"
},
{
"UUID": "9E741BEC-7BC",
"Visible": false,
"WasVisibleTransient": false,
"WhichCorners": 15,
"Width": 796.5216064453125,
"Width2": 796.5216064453125,
"Width3": 796.5216064453125,
"title": "Nice to know,
"Children": [{
"UUID": "9E741BEC-7BC1-49C0-8214-AB5A65762CB7",
"Visible": false,
"WasVisibleTransient": false,
"WhichCorners": 15,
"Width": 796.5216064453125,
"Width2": 796.5216064453125,
"Width3": 796.5216064453125,
"title": "Frances Stowe Great lesson 👌",
"Children": [{
"UUID": "9E741",
"Visible": false,
"WasVisibleTransient": false,
"WhichCorners": 15,
"Width": 796.5216064453125,
"Width2": 796.5216064453125,
"Width3": 796.5216064453125,
"title": "Frances Stowe Great lesson 👌"
}],
}],
},
{
"UUID": "9E741BEC-7BC",
"Visible": false,
"WasVisibleTransient": false,
"WhichCorners": 15,
"Width": 796.5216064453125,
"Width2": 796.5216064453125,
"Width3": 796.5216064453125,
"title": "Nice to know
},
},
The output I would like is below
{
"UUID": "9E741BEC-7BC",
"Visible": false,
"WasVisibleTransient": false,
"WhichCorners": 15,
"Width": 796.5216064453125,
"Width2": 796.5216064453125,
"Width3": 796.5216064453125,
"title": "Nice to know,
"Children": [{
"UUID": "9E741BEC-7BC1-49C0-8214-AB5A65762CB7",
"Visible": false,
"WasVisibleTransient": false,
"WhichCorners": 15,
"Width": 796.5216064453125,
"Width2": 796.5216064453125,
"Width3": 796.5216064453125,
"title": "Frances Stowe Great lesson 👌",
"Children": [{
"UUID": "9E741",
"Visible": false,
"WasVisibleTransient": false,
"WhichCorners": 15,
"Width": 796.5216064453125,
"Width2": 796.5216064453125,
"Width3": 796.5216064453125,
"title": "Frances Stowe Great lesson 👌"
}],
}],
},
{
"UUID": "9E741BEC-7BC",
"Visible": false,
"WasVisibleTransient": false,
"WhichCorners": 15,
"Width": 796.5216064453125,
"Width2": 796.5216064453125,
"Width3": 796.5216064453125,
"title": "Nice to know
},
},
Upvotes: 0
Views: 64
Reputation: 121
You can try this:
function 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( cObj => {
delInRoot(cObj.UUID);
})
removeRootUUID(baseArray, obj.Children);
}
};
}
// arr is your initial array here
removeRootUUID(arr, arr);
console.log(arr);
I'm sure it could be better, but it works with your example
Upvotes: 1