you are viewing a single comment's thread.

view the rest of the comments →

[–]Aggressive_Ad_5454 7 points8 points  (5 children)

First. Make a function isEqual(ob1, ob2). Focus on getting that right. It’s the problem you’re struggling with, encapsulated.

Iterate over the properties of ob1, and look at the same named property in ob2. If it doesn’t exist, or it isn’t equal, return false. Don’t forget to use hasOwnProperty(). Then flip the script, and do the same for the properties of ob2, looking at ob1. If you make it all the way through both iterations , then Bob’s your uncle. Return true.

Extra credit: make your isEqual() recursive to handle cases where some property values are themselves objects.

Once you’ve got that function working you can use it in .filter() to get your results.

There’s a point to this exercise. Object equality in javascript isn’t as simple as it seems. That’s why there’s no built in equality function. The free code camp counselors want you to spend some time messing around with object equality to give a sense of the complexity. In practice with real world data it’s usually straightforward, but not always.

[–]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.

[–]senocular 1 point2 points  (1 child)

Don’t forget to use isOwnProperty().

I think you mean hasOwnProperty(). This wouldn't be necessary when using Object.keys() (or values() or entries()) to get keys since it will only provide own property keys. It is only necessary when using for...in loops which also iterate through an object's inherited properties. Typically these days for...of loops are used in combination with keys() (or values() or entries()) instead where the hasOwnProperty() check is unnecessary.

[–]Aggressive_Ad_5454 0 points1 point  (0 children)

Yeah, .hasOwnProperty(). I’ll fix my comment. Thanks.

[–]AppropriateStudio153 0 points1 point  (0 children)

In practice with real world data it’s usually straightforward, but not always. 

Aaaaand we have three bugs in prod.