Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. /**
  2. * @param items {array} - array from which to remove duplicates
  3. * @param test {function} - function that returns the desired "index" from which to base duplication
  4. * @return {array} - a duplicate free items
  5. *
  6. * @example
  7. * const peeps = [{name:"bob"},{name:"pat"},{name:"pat"},{name:"bob"}];
  8. * removeDuplicates( peeps, item => item.name );
  9. * // [{name:"bob"},{name:"pat"}];
  10. */
  11. function removeDuplicates( items, test ){
  12. const indexes = [];
  13. return items.reduce( (p,c)=>{
  14. const acc = p;
  15. if( indexes.indexOf( test.call(this, c) ) === -1 ) {
  16. indexes.push( test.call( this, c ) );
  17. acc.push( c );
  18. return acc;
  19. }
  20. return p;
  21. },[]);
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement