Skip to content

Instantly share code, notes, and snippets.

@yomotsu
Last active August 29, 2015 14:25
Show Gist options
  • Save yomotsu/509c89f75bc25f948046 to your computer and use it in GitHub Desktop.
Save yomotsu/509c89f75bc25f948046 to your computer and use it in GitHub Desktop.
unique array in pure js
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