travissanon
9/23/2017 - 12:56 AM

freeCodeCamp Intermediat Algorithms: Wherefore Art Thou

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" });