I have developed many complex projects. However, I always get stuck in simple things. Could anyone please guide or share some docs to train on these common logical problems. I have an array of objects and the objects can be duplicate.
Each object has an array property permission: ['add', 'remove']. I want to remove the repeatable object and get its permission array property and merged it with the one we found. Permission array should have unique values.
Please guide also point me to some site or a book which will help me to solve logical problems
const arr = [
{ id: 1, permission: ['add', 'remove'] },
{ id: 2, permission: ['add', 'upload']},
{ id: 2, permission: ['add', 'remove', 'edit'] },
{ id: 3, permission: ['add', 'remove'] },
{ id: 4, permission: ['add', 'remove'] },
{ id: 5, permission: ['add', 'read'] },
{ id: 5, permission: ['read', 'remove'] },
{ id: 6, permission: ['add', 'remove'] },
];
// Result I am looking for.
[
{ id: 1, permission: ['add', 'remove'] },
{ id: 2, permission: ['add', 'remove', 'edit', 'upload'] },
{ id: 3, permission: ['add', 'remove'] },
{ id: 4, permission: ['add', 'remove'] },
{ id: 5, permission: ['add','read', 'remove'] },
{ id: 6, permission: ['add', 'remove'] },
];
// Tried so far
const seen = new Set();
const fa = arr.filter((el) => {
const duplicate = seen.has(el.id);
seen.add(el.id);
return !duplicate;
});
lodash? It has a_mergefeature which I think is doing what you are looking for.