Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. function whatIsInAName(collection, source) {
  2. // "What's in a name? that which we call a rose
  3. // By any other name would smell as sweet.”
  4. // -- by William Shakespeare, Romeo and Juliet
  5. var srcKeys = Object.keys(source);
  6.  
  7. // filter the collection
  8. return collection.filter(function (obj) {
  9. return srcKeys
  10. .map(function(key) {
  11. return obj.hasOwnProperty(key) && obj[key] === source[key];
  12. })
  13. .reduce(function(a, b) {
  14. return a && b;
  15. });
  16. });
  17. }
  18.  
  19. // test here
  20. 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