Skip to content

Instantly share code, notes, and snippets.

@alexandrebvd
Created July 27, 2015 03:44
Show Gist options
  • Save alexandrebvd/813c3b58811e2eea681c to your computer and use it in GitHub Desktop.
Save alexandrebvd/813c3b58811e2eea681c to your computer and use it in GitHub Desktop.
16.Bonfire: Where art thou
function where(collection, source) {
var arr = [];
var findKeys = Object.keys(source);
for (var i = 0; i < collection.length; i++) {
var currentObj = collection[i];
for (var j = 0; j < findKeys.length; j++) {
var key = findKeys[j];
if (currentObj.hasOwnProperty(key) && currentObj[key] === source[key]) {
arr.push(currentObj);
}
}
}
return arr;
}
where([{ first: 'Romeo', last: 'Montague' }, { first: 'Mercutio', last: null }, { first: 'Tybalt', last: 'Capulet' }], { last: 'Capulet' });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment