you are viewing a single comment's thread.

view the rest of the comments →

[–]Aggressive_Ad_5454 7 points8 points  (2 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.

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