0

My strategy is removing duplicated data from array and show data just not duplicated.

const array1 = [{id:1 , name:"John" , age:20}, 
                {id:2 , name:"Sam" , age:15},
                {id:3 , name:"Paul" , age:28},
                {id:4 , name:"David" , age:50},
                {id:1 , name:"John" , age:20},
                {id:4 , name:"David" , age:50}]

/*Expected result is  

array1 = [{id:2 , name:"Sam" , age:15},
          {id:3 , name:"Paul" , age:28]

*/

Thank you.

CHAINECS
  • 1
  • 1
  • In my school there was a sign that said "the cleaner place is not the place that is cleaned the most, but the place that get dirty the less". Said that, you should avoid inserting duplicated elements. If not possible, then you can use a set element and return that set as an array, using always some valid key, in your case the id. It may not be the best solution, but it will work. `const elementsSet = new Set() const filteredArray = array1.filter(el => { if (elementsSet.has(el.id)) { return false } else { elementsSet.add(el.id) return true } })` – Kenyi Larcher Jun 27 '22 at 20:58
  • Thank you so much for your answer ,but that is not my expected result. My expected result is array1 = [{id:2 , name:"Sam" , age:15}, {id:3 , name:"Paul" , age:28}] or filteredArray = [{id:2 , name:"Sam" , age:15},{id:3 , name:"Paul" , age:28}] – CHAINECS Jul 06 '22 at 09:07

0 Answers0