Last active
August 29, 2015 14:25
-
-
Save yomotsu/509c89f75bc25f948046 to your computer and use it in GitHub Desktop.
unique array in pure js
This file contains hidden or 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
var a = [ {x:1}, {x:2}, {x:1}, {x:1}, {x:3} ]; | |
console.log( unique( a ) ); // -> [ {x:1}, {x:2}, {x:3} ] | |
function unique ( array ) { | |
var result = []; | |
array.forEach( function ( el ) { | |
var unique = !result.some( function ( el2 ) { return el.x === el2.x } ); | |
if ( unique ) { | |
result.push( el ); | |
} | |
} ); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment