-1

I have two arrays. My goal is to get a third array with users that exist in usersUpdated but do not exist in oldUsers. I know i have to apply multiple filters, but I do not know how.

   const oldUsers = [
     { name: 'Fede', id: 1 },
     { name: 'Marce', id: 2 },
  ];

   const usersUpdated = [
   { name: 'Fede', id: 1 },
   { name: 'Marce', id: 2 },
   { name: 'Ale', id: 3 },
   { name: 'Julian', id: 4 },
];


const expectedValue = [
  { name: 'Ale', id: 3 },
  { name: 'Julian', id: 4 }
 ];
  • 3
    Why do you need multiple filters? Just filter `usersUpdated` with the condition that it doesn't exist in `oldUsers`. – Barmar Sep 29 '21 at 22:35
  • Your question is unclear : which is the reference for duplicates ? the Id or the name, or both ? if the Id is the same but the names arre differentd sould the name to be updated ? – Mister Jojo Sep 29 '21 at 22:41
  • plus : in your examples, the tables are listed in ascending order on the value of the Id. is this a mandatory rule? – Mister Jojo Sep 29 '21 at 22:46

1 Answers1

0

Convert arrays to objects, use names as keys & ids as values, delete keys of old obj from updated obj then convert the updated obj back to array.

huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97