Skip to content

Instantly share code, notes, and snippets.

@nicolasmendonca
Forked from Bradshaw/union.js
Last active March 17, 2019 12:38
Show Gist options
  • Save nicolasmendonca/d83a856e6745996ba7bca1a7f8766948 to your computer and use it in GitHub Desktop.
Save nicolasmendonca/d83a856e6745996ba7bca1a7f8766948 to your computer and use it in GitHub Desktop.
Joins two arrays while removing duplicate items
Array.prototype.union = ( array ) => {
const concatenated = this.concat( array );
return concatenated.filter( ( item, pos ) => concatenated.indexOf( item ) === pos );
};
// example
[ 'a', 'b', 'c' ].union([ 'b', 'c', 'd', 'e' ]); // [ 'a', 'b', 'c', 'd', 'e' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment