Uncategorized

Wherefore art thou

Original Problem

Make a function that looks through an array of objects (first argument) and returns an array of all objects that have matching property and value pairs (second argument). Each property and value pair of the source object has to be present in the object from the collection if it is to be included in the returned array.

For example, if the first argument is [{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], and the second argument is { last: "Capulet" }, then you must return the third object from the array (the first argument), because it contains the property and its value, that was passed on as the second argument.

function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
  
  
  // Only change code above this line
  return arr;
}

whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });


This is another interesting and tricky intermediate algorithm challenge. While the instructions were clear, I wasn’t sure where to start. I knew I needed to find a way to check to see if the object from the second argument was present anywhere in the first argument.

What I like to do is start writing comments of what I think I should do and/or write out some ES6 array methods to get some things on the screen (to get started and to practice to make sure I’m using them correctly). Below are some of my notes making sure I was using .map and .filter correctly followed by an attempt to compare the arguments with a nested for loop.

const whatIsInAName = (collection, source) => {
  // What's in a name?
  let arr = [];
  // Only change code below this line
  let name = collection.map(obj => obj.first === "Tybalt" ? obj.last : "Not a match");
  console.log(name);
  
  let name2 = collection.filter(obj => obj.first === "Tybalt");
  console.log(name2);
  
for (let i = 0; i < collection.length; i++) {
  for (let j = 0; j < source.length; j++) {
    if (collection[i] === source[i]) {
      console.log(collection[i]);
    }
  }
}
  
  // Only change code above this line
  return arr;
};

whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });

Basic Solution

function whatIsInAName(collection, source) {
  
  var srcKeys = Object.keys(source);

  // filter the collection
  return collection.filter(function (obj) {
    for(var i = 0; i < srcKeys.length; i++) {
      if(!obj.hasOwnProperty(srcKeys[i]) || obj[srcKeys[i]] !== source[srcKeys[i]]) {
        return false;
      }
    }
    return true;
  });
}

// test here
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });

The basic solution reminded me of a few different object properties that I hadn’t used in awhile and obviously didn’t consider using.

In a nutshell, this solution uses Object.keys to get an array of the key of the second argument, (making srcKeys equal to [ ‘last’]) then filters through collection, using a for loop and an if statement to see if the object in the collection argument doesn’t have the key and the property value doesn’t match the value in the source argument.

Leave a comment