Skip to content

Instantly share code, notes, and snippets.

@rickMcGavin
Last active August 29, 2016 00:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rickMcGavin/101eda858435897de79cf3ff890af464 to your computer and use it in GitHub Desktop.
Save rickMcGavin/101eda858435897de79cf3ff890af464 to your computer and use it in GitHub Desktop.
// 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