Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @param items {array} - array from which to remove duplicates
- * @param test {function} - function that returns the desired "index" from which to base duplication
- * @return {array} - a duplicate free items
- *
- * @example
- * const peeps = [{name:"bob"},{name:"pat"},{name:"pat"},{name:"bob"}];
- * removeDuplicates( peeps, item => item.name );
- * // [{name:"bob"},{name:"pat"}];
- */
- function removeDuplicates( items, test ){
- const indexes = [];
- return items.reduce( (p,c)=>{
- const acc = p;
- if( indexes.indexOf( test.call(this, c) ) === -1 ) {
- indexes.push( test.call( this, c ) );
- acc.push( c );
- return acc;
- }
- return p;
- },[]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement