Skip to content

Instantly share code, notes, and snippets.

@travissanon
Created September 23, 2017 00:56
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 travissanon/9b106dcfffeef5d334179f52e11506b4 to your computer and use it in GitHub Desktop.
Save travissanon/9b106dcfffeef5d334179f52e11506b4 to your computer and use it in GitHub Desktop.
freeCodeCamp Intermediat Algorithms: Wherefore Art Thou
'esversion: 6';
function whatIsInAName(collection, source) {
// Stores the name of each nameValue pair of the source object in an array
var sourceKeys = Object.keys(source);
// Iterates through the collection array
return collection.filter(item => {
// For every key in the sourceKeys variable
return sourceKeys.every(key => {
// If the item has the same property name as the name as the key
// AND if the value of the item is the same as the value of the source
return item.hasOwnProperty(key) && item[key] === source[key];
});
});
}
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { first: "Tybalt", last: "Capulet" });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment