DEV Community

Discussion on: Remove duplicates from an Array the short & sweet way!

Collapse
 
ankit199 profile image
Ankit kumar shukla

is this work with like array data [{id=1,name='a'},{id=1,name='b'},{id=2,name='c'},{id=3,name='d'},{id=2,name='e'},{id=3,name='f'},{id=4,name='ag'},{id=5,name='au'}]

Collapse
 
ajmalhassan profile image
Ajmal Hassan

No, it won't.

The above method only works for Primitive Types (number, string, boolean, null, undefined) in the array.

so,

const primitivesArr = [true, false, true, false, undefined, 1, undefined, 'hello', 1, 'hello']

console.log([...new Set(primitivesArr)])
// output: [true, false, undefined, 1, "hello"]

For objects, you'll have to use filter()

Collapse
 
ankit199 profile image
Ankit kumar shukla

okk ..thank you