DEV Community

Discussion on: Finding a single item in an array

Collapse
 
gilfewster profile image
Gil Fewster

Postscript

Removing duplicate items from your array
EG convert [1,2,2,1,4,8,8,9] to [1,2,4,8,9]

Create a new set from your original array and then spread the set into a new array.

const animals = ["cat","dog","monkey","cat"]
const nums = [1,7,2,4,1,1,2,7,2,4];

const uniqueAnimals = [...new Set(animals)]; // ["cat","dog","monkey"]
const uniqueNums = [...new Set(nums)]; // [1,7,2,4]

Enter fullscreen mode Exit fullscreen mode