Skip to content

Instantly share code, notes, and snippets.

@samkahchiin
Last active March 27, 2016 10:00
Show Gist options
  • Save samkahchiin/30acbb46315afdfdfa4e to your computer and use it in GitHub Desktop.
Save samkahchiin/30acbb46315afdfdfa4e to your computer and use it in GitHub Desktop.
freecodecamp - where art thou
function where(collection, source) {
var arr = [];
// What's in a name?
var key = Object.keys(source);
for( var i = 0; i < collection.length; i++ ){
var counter = 0;
for (var k = 0; k < key.length; k++){
if(source[key[k]] === collection[i][key[k]]){
counter++;
}
}
if( counter == key.length ){
arr.push(collection[i]);
}
}
return arr;
}
where([{ "a": 1, "b": 2 }, { "a": 1 }, { "a": 1, "b": 2, "c": 2 }], { "a": 1, "b": 2 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment