MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/learnjavascript/comments/oe71tv/remove_duplicates_from_arrays_using_set/h46srcf/?context=3
r/learnjavascript • u/webdevolution_ • Jul 05 '21
44 comments sorted by
View all comments
3
Only works on primitive values like strings, numbers, and booleans.
0 u/FriesWithThat Jul 06 '21 If someone has a use case for it, they can create a new array with all identical values removed with something like this:const data= [ 1, 2, 1, { id: 10, other: 'blah' }, { id: 15, other: 'meh' }, { id: 10, other: 'blah' }, ]; const newObj = {}; const filterValues = data.filter(el => { if (!newObj[JSON.stringify(el)]) { newObj[JSON.stringify(el)] = true; return el; } return false; }); console.log('filterValues:', filterValues); 3 u/nadameu Aug 01 '21 This might not work if "id" and "other" are declared in a different order when creating the object.
0
If someone has a use case for it, they can create a new array with all identical values removed with something like this:
const data= [ 1, 2, 1, { id: 10, other: 'blah' }, { id: 15, other: 'meh' }, { id: 10, other: 'blah' }, ]; const newObj = {}; const filterValues = data.filter(el => { if (!newObj[JSON.stringify(el)]) { newObj[JSON.stringify(el)] = true; return el; } return false; }); console.log('filterValues:', filterValues);
3 u/nadameu Aug 01 '21 This might not work if "id" and "other" are declared in a different order when creating the object.
This might not work if "id" and "other" are declared in a different order when creating the object.
3
u/HashFap Jul 05 '21
Only works on primitive values like strings, numbers, and booleans.