Implement a Matching Object Filter - Contains Hints and Solution

I was trying to find a solution to this lab using built in JavaScript methods and avoiding any for loops with custom logic. It took me a while, but after combing through the MDN docs. I was able to find a solution.

Hints:

  • Look into JSON static methods.

  • Think about how you can manipulate strings so that you can compare the source object with the objects in the array.

  • How can you use some built in Array methods to filter objects in the array that include all the key and value pairs present in the source object.

I am open to feedback on my solution, If you can suggest any improvements please let me know!

function whatIsInAName(arr, sourceObj) {
  const sourceObjEntries =  JSON.stringify(sourceObj).replaceAll(/[{}]/g,"").split(",");
  sourceObjEntries.forEach((srcObj) => {
    arr = arr.filter((obj) => JSON.stringify(obj).includes(srcObj));
  })

  return arr;
}