Last active
August 29, 2016 00:37
-
-
Save rickMcGavin/101eda858435897de79cf3ff890af464 to your computer and use it in GitHub Desktop.
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
// FreeCodeCamp | |
// Intermediate Algorithm Scripting | |
// Wherefore Art Though | |
function whatIsInAName(collection, source) { | |
// What's in a name? | |
var keys = Object.keys(source); | |
return collection.filter(function(obj) { | |
return keys.every(function(key) { | |
return obj.hasOwnProperty(key) && obj[key] === source[key]; | |
}); | |
}); | |
} | |
console.log(whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, | |
{ first: "Tybalt", last: "Capulet" }], { last: "Capulet" })); | |
// returns [{ first: "Tybalt", last: "Capulet" }] | |
console.log(whatIsInAName([{ "a": 1, "b": 2 }, { "a": 1 }, { "a": 1, "b": 2, "c": 2 }], { "a": 1, "b": 2 })); | |
// returns [{ "a": 1, "b": 2 }, { "a": 1, "b": 2, "c": 2 }] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment