DEV Community

Discussion on: Code Snippet Series: Get Unique Values from Array

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt • Edited

jsperf.com/unique-arrays-1

Objects won't work with non-string keys, as well as nasty strings (e.g. __proto__).

Collapse
 
functional_js profile image
Functional Javascript

Correct, these funcs work with the primitives (string and number).

Ref types (comparing objects and arrays) require custom logic, even if working with Set and Map.

eg...

const aDups = [{ id: 1 }, { id: 1 }, { id: 1 }, { id: 1}];
const setDups = new Set(aDups)
console.log(setDups)

// output:
// Set(4) { { id: 1 }, { id: 1 }, { id: 1 }, { id: 1 } }