Created
September 23, 2017 00:56
-
-
Save travissanon/9b106dcfffeef5d334179f52e11506b4 to your computer and use it in GitHub Desktop.
freeCodeCamp Intermediat Algorithms: Wherefore 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
'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