Skip to content

Instantly share code, notes, and snippets.

@Greenie0506
Last active August 29, 2015 14:17
Show Gist options
  • Save Greenie0506/03705dd8a9188ad19d34 to your computer and use it in GitHub Desktop.
Save Greenie0506/03705dd8a9188ad19d34 to your computer and use it in GitHub Desktop.
// This is where im using the unique function
var result = boxxspring.arrayUnique( results[1].concat( artifacts ), function( a, b ) {
return a.id == b.id ? 0 : 1;
});
// this is the definition of the unique array function
boxxspring.arrayUnique = function( array, comparator ) {
var comparator = comparator || function( a, b ) {
return a == b ? 0 : 1;
}
var result = new Array();
source:
for( var index = 0, length = array.length; index < length; index++ ) {
for( var resultIndex = 0, resultLength = result.length; resultIndex < resultLength; resultIndex++ ) {
if ( !comparator( result[ resultIndex ], array[ index ] ) ) {
continue source;
}
}
result[ result.length ] = array[ index ];
}
return result;
}
@jordanorelli
Copy link

boxxxspring.arrayUnique = function(array, fn) {
    var out = [];
    var known_ids = {};
    for (var i = 0; i < array.length; i++) {
        var id = fn(array[i]);
        if (known_ids.hasOwnProperty(id)) {
            continue;
        }
        known_ids[id] = true;
        out.push(array[i]);
    }
    return out;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment