Remove duplicate data from an object array

Asked

Viewed 29 times

0

I would like to know how do I remove duplicate data from an object array. I tried it the way below, if there is another way better please tell me :

I first created two arrays and then merged the two

let firstArray = [
      { name: "javascript", id: 1 },
      { name: "golang", id: 2 },
      { name: "python", id: 3 },
    ];

    let secondArray = [
      { name: "javascript", id: 1 },
      { name: "golang", id: 2 },
      { name: "python", id: 3 },
      { name: "ruby", id: 4 },
      { name: "elixir", id: 5 },
      { name: "rust", id: 6 },
    ];

const merge = [...firstArray,...secondArray];

I used the SET method to remove duplicates :

 const unique = [...new Set(merge.map((language)=> language.id  ))]
    
 console.log(unique) // [1, 2, 3, 4, 5, 7]

That way he removed the duplicates, but the output of it brings me only the ids and I would like it to also bring the name.

  • It is because Voce is returning the values of each language.id in the map array

  • How can I return data with name and id? I tried to pass language instead of language.id on the map, but its output returns all items (including duplicates).

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.