Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function whatIsInAName(collection, source) {
- // "What's in a name? that which we call a rose
- // By any other name would smell as sweet.”
- // -- by William Shakespeare, Romeo and Juliet
- var srcKeys = Object.keys(source);
- // filter the collection
- return collection.filter(function (obj) {
- return srcKeys
- .map(function(key) {
- return obj.hasOwnProperty(key) && obj[key] === source[key];
- })
- .reduce(function(a, b) {
- return a && b;
- });
- });
- }
- // test here
- whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement