ReactiveX / learnrx

A series of interactive exercises for learning Microsoft's Reactive Extensions Library for Javascript.
1.4k stars 292 forks source link

Object comparison method #7

Open axiomabsolute opened 11 years ago

axiomabsolute commented 11 years ago

On exercise 24, I wrote my solution, ran it and... didn't match the expected output. I started debugging, I printed out each of my results, and they looked correct. So then I showed the solution, and compared each property of each of the resulting objects... also correct. So I printed both to the console; they also matched. Then I finally admitted defeat and actually looked at the solution code. The only difference I could see was the following:

solution:

return {id: video.id, title: video.title, time: interestingMoment.time, url: boxart.url}

mine:

return {id: video.id, title: video.title, url: boxart.url, time: interestingMoment.time}

In case you weren't reading closely, the only difference was the order the url and time properties were added to the returned object. I swapped the order and, lo and behold, it works. Looking at the source code, it looks like this is because the results are compared by sorting the resulting array, stringifying the results, and then comparing the result strings. Unfortunately, most browsers iterate over object properties in the order they were added, so two objects which have the exact same properties and values will NOT produce the same JSON.stringify strings.

Is there any particular reason to avoid doing property based comparisons, that is, checking to ensure both objects have the exact same set of property keys and associated values, as opposed to using the string-based comparison? I would be happy to implement the changes and submit a pull request if that would be amenable.

Thanks

jhusain commented 11 years ago

This was laziness and time constraints, pure and simple. A pull request would be very valuable as I'm sure others have hit the same issue.