//remove doubles from an array of primitive values
console.log(
"no doubles primitive values",
[1,2,3,4,3,4,5,6,7,7,7,8,9,1,2,3,4].filter(
(value,index,all)=>all.indexOf(value)===index
)
);
//remove doubles from an array of references you can do the same thing
const one = {id:1}, two = {id:2}, three = {id:3};
console.log(
"no doubles array of references",
[one,two,one,two,two,one,three,two,one].filter(
(value,index,all)=>all.indexOf(value)===index
)
);
//remove doubles from an array of things that don't have the same reference
// but have same values (say id)
console.log(
"no doubles array of things that have id's",
[{id:1},{id:1},{id:1},{id:2},{id:3},{id:2}].reduce(
([all,ids],item,index)=>
[ all.concat(item), ids.concat(item.id) ],
[[],[]]
).reduce(
(all,ids)=>
all.filter(
(val,index)=>ids.indexOf(val.id)===index
)
)
);