-
-
Save nicolasmendonca/d83a856e6745996ba7bca1a7f8766948 to your computer and use it in GitHub Desktop.
Joins two arrays while removing duplicate items
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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