Wherefore Art Thou

Today’s function will focus on objects and not shakespeare.

The goal is to 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 of the second object (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.

Before we begin let’s look at shakespeare as our newly created empty array:

Look at those snugly tights

But we will also create a variable that will contain a boolean. This will be useful later in the function and it will be set to false.

In the function, you get two inputs. One is called collection and the other a source.

In the function description, the collection is an array of objects, while the source is an object containing a property and value pairs:

Let’s loop into the collections array first using for…in and then inside that for loop, we’ll use another for…in to loop through the object. We are using a for…in instead of a regular for loop because the for…in statement iterates over the enumerable properties of an object, in arbitrary order:

Let’s look at how the for…in loop will work in making sure that every property and value pair exist in each object in the collection array:

For the first item, we have the first object in the collection array. Now we will nest a for…in loop inside the collections loop.

This is so each object inside the collections array can be evaluated against the source object. Inside the source loop, we see the first property value pair:

Using the .hasOwnProperty method, this will determine if whether the object has the specified property. As highlighted red, the first object in the collection array has the “a” property. Now we need to determine if they have the same value (either using bracket or dot notation).

Long behold, they have the same value (assuming you use comparisons and an if statement to determine this).

Let’s move to the next property and value pair in the source object:

Using hasOwnProperty, you determine of the first object in the collections array has a property of “b”:

They do. Let’s see if they have the same value:

And once again, they have the same property and value. Remember the variable that has a boolean value that was created in the beginning of the function (outside the for loops). This is useful because if there was a successful match between the particular object in the collections array and it has all the property and value pairs that the source object has, then that variable will change to true.

If there wasn’t a match, the variable will remain false and it will be better to “break” out of the loop as what matters is if the object contains all property and value pairs that are in source.

Now we are done going through the source object for the first object in the collections array, let’s leave the inner loop and move onto the second object in the collections array:

Now it is time to start the for…in loop for the source object and start comparing:

Using the same steps with the first collection object, we use hasOwnProperty method to see if the second object has an “a” property:

Let’s see if they have the same value using dot or bracket notation to get the value:

…and they do but let see what happens when we try to see if this same collection object has a “b” property:

Well. The object doesn’t have a B property and therefore, our boolean variable becomes a false and we break out of the loop:

We are not going to repeat the same process with the third object in the collections array. You know by looking at it, this boolean variable should become a true because it contains all the property and value pairs that exist in the source object:

And that means by the time this third collections object leaves the nested for…in loop, the boolean variable becomes:

At the end, any object in the collections array that comes out of the nested for…in loop for the source object with a boolean variable yielding to true gets added to the empty shakespeare array. The array we created in the beginning of the function (his picture is in the beginning of the article).

In the end, with the selected inputs, the output of the function should be:

Like what you read? Give Erica N a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.