Nice example. Also, another way:
let sett = new Set(); return arr.map((v, index) => { if(sett12.has(v.id)) { return false } else { sett12.add(v.id); return index; } });
Or if we want just unique values then:
function noDuplicates(arr) { return [...new Set(arr.map(v => v.id))]; }
wow, good solution.
The final solution with typos fixed.
function getUnique(arr){ let set = new Set(); return arr.map((v, index) => { if(set.has(v.id)) { return false } else { set.add(v.id); return index; } }).filter(e=>e).map(e=>arr[e]); }
This was a great article. This helped me with my project. I was using faker to create some data but faker created duplicate names in an array of objects, but the client didn't want any duplicates.
Worked perfectly Thanks!!
Still trying to understand the code though.
Especially .filter(e=>e).map(e=>arr[e]);
Gotta dig deeper with map and filter!
filter(e=>e) part fails if e (index) is 0.
Should be: filter(e => e || e === 0)
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Nice example. Also, another way:
Or if we want just unique values then:
wow, good solution.
The final solution with typos fixed.
This was a great article. This helped me with my project. I was using faker to create some data but faker created duplicate names in an array of objects, but the client didn't want any duplicates.
Worked perfectly Thanks!!
Still trying to understand the code though.
Especially
.filter(e=>e).map(e=>arr[e]);
Gotta dig deeper with map and filter!
filter(e=>e) part fails if e (index) is 0.
Should be: filter(e => e || e === 0)