back to Overview

.equals()

Checks whether two values, lists or objects are equal in a deep comparison.
Util module only.

Syntax Variants

list.equals(otherObject)
_.equals(thisObject, otherObject)

Parameters

thisObject
The first reference to evaluate.
otherObject
The second reference to evaluate.
(return value)
true if both references are equal. False otherwise.

Description

Checks whether two values, lists or objects are equal in a deep comparison.

First equals() checks whether it got a function as parameter. If yes, it will be invoked without arguments and equals() calls itself recursively with the function's result.

Once both values are no functions anymore, the values will be evaluated, If the first value is...

  • ...null or undefined, they are only equal if the other one is also either null or undefined.
  • ...a value as defined by _.isValue(), but not a Date, they are equal if the other value is the same type and is equal according to the '==' operator.
  • ...a Date, they are equal if the other value is a Date representing the same time.
  • ...a list or array, they are equal if the other value is also either a list or an array, has the same number of items and all items equal the items of the other list at the same position. The equality of list items is determined recursively using the same rules, so you can also nest lists.
  • ...a function, it will be invoked without arguments and its return value is evaluated using these rules as if the value has been passed.
  • ...any other object, they are equal if they contain exactly the same keys (as defined by _.eachObj()) and all values are equal as determined using these rules recursively.

Please note that, according to the rules, a Minified list is equal to an array, as long as their content is equal. equals does not differentiate between null and undefined.

equals is commutative. If you swap the parameters, the result is the same as long as no functions are involved.

Example

Compare a list and an array:

 _.equals([1, 2, 3], _(1, 2, 3));  // returns true

Example

Same result, but with a list method:

 _(1, 2, 3).equals([1, 2, 3]);  // returns true

Comments

comments powered by Disqus

back to Overview

Functions