-
-
Save nirajkrz/3931d27ca25a85dbefea to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/nirajkrz 's solution for Bonfire: Where art thou
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Bonfire: Where art thou | |
// Author: @nirajkrz | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-where-art-thou | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function where(collection, source) { | |
var arr = []; | |
// What's in a name? | |
var keys = Object.keys(source); | |
// Filter array and remove the ones that do not have the keys from source. | |
arr = collection.filter(function(obj) { | |
//Use the Array method every() instead of a for loop to check for every key from source. | |
return keys.every(function(key) { | |
// Check if the object has the property and the same value. | |
return obj.hasOwnProperty(key) && obj[key] === source[key]; | |
}); | |
}); | |
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