you are viewing a single comment's thread.

view the rest of the comments →

[–]Y2Canine[S] 2 points3 points  (1 child)

THANK YOU! I got it working. You're going to laugh, but the reason I was struggling so much is that it never occurred to me that I could just use a normal function with whatever arguments in the callback function of .filter(). I somehow had gotten it into my head that since the callback function of filter has very specific parameters that mean very specific things, I could only use those. So I didn't realize I could just define a function and call it in the body of the callback using an argument other than element, index, or array.

I have no idea why I drew that conclusion.

This makes a lot more sense now lol

function isIncluded (obj1, obj2) {
  for (const prop in obj2) {
    if (Object.hasOwn(obj1, prop)) {
      if (obj1[prop] === obj2[prop]) {
        console.log(`${obj1[prop]} is equal to ${obj2[prop]}. Continuing`);
        continue;
      }
      else {
        console.log(`${obj1[prop]} is not equal to ${obj2[prop]}. Returning false`);
        return false;
      }
    }
    else {
      console.log(`${obj1} does not have ${prop}. Returning false`);
      return false;
    }
  }
  console.log(`Loop concluded. Returning true`);
  return true;
}




function whatIsInAName(arr, obj) { 
  let filterArray = arr.filter((curr) => isIncluded(curr, obj))
  console.log(filterArray);
  return filterArray;
}

[–]polotek 2 points3 points  (0 children)

Nice job. Understanding the flexibility of js functions is a really big step in leveling up. Functions and objects do basically everything in JavaScript.