DEV Community

[Comment from a deleted post]
Collapse
 
squgeim profile image
Shreya Dahal • Edited

If you are going to use filter, it'd be better to make the filter method pure.

[1,2,1,2,35,3,4].filter((v, i, arr) => arr.indexOf(v) === i)

That way, you can extract the function out:

function makeUniq(v, i, arr) {
  return arr.indexOf(v) === i;
}

[1,2,3,4,4,3,4].filter(makeUniq);
[6,7,3,4,6,7,7].filter(makeUniq);

Also, map is supposed to be pure, use forEach to do mutations instead.