1

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;
});
1
  • 1
    I'm not sure if the usage of external libraries is allowed here, but have you looked into lodash? It has a _merge feature which I think is doing what you are looking for. Commented Jun 19, 2020 at 4:39

4 Answers 4

1

Use array.reduce to create a hash of ids and then access the values of it using Object.values(),

Please find the code in attached snippet

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'] },
];

const result = arr.reduce((acc, {id, permission}) => {
    if (!acc[id]) {
      acc[id] = {
        id,
        permission: [...new Set(permission)],
      };
    }
    acc[id] = { 
      id,
      permission: [...new Set([...acc[id].permission, ...permission])],
    };

    return acc;
  }, {});

console.log(Object.values(result));

Sign up to request clarification or add additional context in comments.

Comments

1

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'] },
];
 

function run(arr) {
  return arr.reduce((previousValue ,currentValue) => {
    let item = previousValue.find(e => e.id === currentValue.id)
    if (item) {
      item.permission =  [...new Set([...item.permission, ...currentValue.permission])]
      return previousValue
    } else {
      return previousValue.concat(currentValue)
    }
  },[])
}
console.log(run(arr))

1 Comment

upload permission is missing for id 2
0

This will work!!!

var 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'] },
];

var res = [];

for (var i = 0; i< arr.length; i++) {
    if(res.length == 0) res.push(arr[i]);
    else {
       if ( !res.find(item => item.id == arr[i]['id'])) {
            res.push(arr[i]);
       }
        else  {
            var resIndex = res.findIndex(item => item.id == arr[i]['id']);
            for(var j=0; j<arr[i]['permission'].length; j++) {
                if(res[resIndex]['permission'].indexOf(arr[i]['permission'][j]) === -1) res[resIndex]['permission'].push(arr[i]['permission'][j]);
            }
        }
    }
}

console.log(res);

Comments

0

    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'] }
    ];
    
    let i;
    arr.forEach(function (arrkey, index) {
      for (i = index + 1; i < arr.length; ++i) {
        if (arrkey.id === arr[i].id) {
          arr[i].permission.forEach(function (per) {
            if (!arrkey.permission.includes(per)) {
              arrkey.permission.push(per);
            }     
          });
          arr.splice(i, 1);
        }
      }
    });
    
    console.log(arr);

This simple solution should work.

1 Comment

Related to logical problems, I think maybe you have not worked on javascript projects? It's ok to feel a bit bad about it but with practice, you will be able to overcome these easily. Try some logical problems online ( leetcode, codeforces ) etc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.