Skip to content

Instantly share code, notes, and snippets.

@harshitanand
Created December 4, 2015 09:49
Show Gist options
  • Save harshitanand/265cf06a1cf1ba39e71d to your computer and use it in GitHub Desktop.
Save harshitanand/265cf06a1cf1ba39e71d to your computer and use it in GitHub Desktop.
// Bonfire: Where art thou
// Author: @harshitanand
// Challenge: http://www.freecodecamp.com/challenges/bonfire-where-art-thou
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function isSuper(a,b){
var l=b.length,i=0,c;
if(l>a.length){return false;}
else{
for(i;i<l;i++){
c=a.indexOf(b[i]);
if(c>-1){
a.splice(c,1);
}
else{return false;}
}
return true;
}
}
function where(collection, source) {
var arr = [];
var sourcekey = Object.keys(source);
for ( var i = 0; i < collection.length; i++ ) {
var collectionkey = Object.keys(collection[i]);
if ((isSuper(collectionkey,sourcekey)) || (collectionkey.length >= sourcekey.length)){
if ( collection[i][sourcekey] === source[sourcekey] ) {
arr.push(collection[i]);
}
}
}
return arr;
}
where([{ "a": 1, "b": 2 }, { "a": 1 }, { "a": 1, "b": 2, "c": 2 }], { "a": 1, "c": 2 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment