How to Remove Duplicates from Array in Javascript ES6

How to Remove Array Duplicates in ES6

Below is the sample array we have, class7 comes two times.

var arr = ["class5","class7","class2","class7"];

we want to remove the duplicates value

var result = [...new Set(arr)];

// result = ["class5","classs7","class2"]

Now lets assume we have array object and we need to remove objects which has duplicate id

var arr = [
  {id: 1, name: "test"},
  {id: 2, name: "test2"},
  {id: 1, name: "test3"},
  {id: 3, name: "test4"}
]
var result = [...new Set(arr.map(({id}) => id))].map((val) => arr.find(({id}) => id === val));

Comments

Popular posts from this blog

world tourism guide Template

Es6 how to check if value is not null or undefined?