Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to compare two objects?

edited April 2016 in Help! with 2.0
So let's say we have object "foo":
<<set $foo to { favouriteGame : "Asteroids" }>>
And we also have an object "bar":
<<set $bar to { favouriteGame : "Asteroids" }>>
Is there a function or method (like .equals()?) that I can use that compares two objects and returns true/false based on whether or not their contents are equal?
If there is and I just couldn't find it, does it compare sub-objects, too?

Comments

  • Not built in, no.

    If you actually need such a feature, then you could download the production build of Underscore—it's the "Production Version (x.x.x)" link under the downloads section.

    Paste the content of that into your Story JavaScript, with one minor change required to make it work. At the very end of the library, just before the source map comment at the end of a very long line, you'll find a call() method invocation which you need to modify.

    FIND:
    .call(this);
    //# sourceMappingURL=underscore-min.map
    
    REPLACE WITH:
    .call(window);
    //# sourceMappingURL=underscore-min.map
    

    Once done, you should be able to use any of the Underscore library methods. The one you're looking for is named isEqual. For example:
    /* Just print the conditional result (true or false). */
    Objects are equivalent: <<print _.isEqual($foo, $bar)>>
    
    /* Within an <<if>>. */
    Objects <<if _.isEqual($foo, $bar)>>are<<else>>are NOT<</if>> equivalent.
    
  • Thanks a lot, this is exactly what I was looking for.
Sign In or Register to comment.