I'm writing JavaScript unit tests (with the QUnit library). I need to verify that my array contains expected (and only) elements.
var array = getArrayFunction(a, b);
equal(["one", "two", "three"], array, "Test is failing even if 'array' contains needed elements");
What would be the easiest way to do this?
Programmers who wish to compare the contents of two arrays must use the static two-argument Arrays. equals() method. This method considers two arrays equivalent if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equivalent, according to Object. equals() .
However for future reference there is a simple way to compare two arrays OF THE SAME TYPE AND SIZE and that is to use the memcmp() function which compares two blocks of memory and returns an integer greater than 0 if the first block is greater than the second, returns 0 if the two blocks are the same, and an integer ...
Compare Arrays in C# Using == (Equality Operator) This method is going to receive the two arrays we want to compare as parameters. After checking if they are equal using == , it is going to return a bool indicating the result of this operation.
Using Arrays. equals(array1, array2) methods − This method iterates over each value of an array and compare using equals method. Using Arrays. deepEquals(array1, array2) methods − This method iterates over each value of an array and deep compare using any overridden equals method.
You should use deepEqual()
in lieu of equal()
. That will compare array elements and object properties, not just use the ==
comparison operator, which evaluates to false
for objects that don't share the same constructor.
Docs here: https://api.qunitjs.com/deepEqual/
More information about JavaScript equality comparison: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With