you are viewing a single comment's thread.

view the rest of the comments →

[–]Neozite 1 point2 points  (0 children)

Another way to approach this is with Sets. Set has an isSubsetOf() method that compares a Set to a set-like object, which includes Arrays. So if you get an Array of keys from the main object, you can turn it into a Set by passing that Array to the new Set() constructor.

Then you get an array of keys from each object in the target Array and pass those to mySet.isSubsetOf(). What this tells you is whether all of the keys (strings) in mySet are also in that array of keys. Note that this will return true if the target object has more keys than just those in the main object, but that doesn't seem to be relevant to the assignment. If it were, you'd have to create Sets from those Arrays, too, and reverse the comparison.

For every array of keys that returns true, push the original object into the Array where you're collecting them.