20

I have an array of arrays, which looks something like this:

[["Some string", "Some other string"],["Some third string", "some fourth string"]]

I think I can use the _.all method in Underscore to determine if all of the arrays match 100% (that is all of their values match), but I'm not sure how to write the required iterator to run the check.

Anyone have an idea?

9 Answers 9

31

Why not intersection? (if you really want to use some Underscore functions for this) http://underscorejs.org/#intersection

If the arrays are of the same length, and the length of the intersection equals to the length of the arrays, then they all contain the same values.

Sign up to request clarification or add additional context in comments.

1 Comment

I believe this only works if you assume array values are unique
25

My preference:

_.isEqual(_.sortBy(first), _.sortBy(second))

And if order matters:

_(first).isEqual(second)

Comments

14

Try this guy (order-independent):

function allArraysAlike(arrays) {
  return _.all(arrays, function(array) {
    return array.length == arrays[0].length && _.difference(array, arrays[0]).length == 0;
  });
}

This is assuming you want all of the arrays to contain all the same elements in the same order as one another (so for your example input the function should return false).

3 Comments

and if you don't want to use _ ?
_.difference only checks if the values of the first array are not present in the second array; additional values in the second array are not detected; if you really want to make sure that both arrays are the same you should do _.difference(array, arrays[0]).length == 0 && _.difference(array[0], arrays).length == 0
allArraysAlike( [ [0,1] , [0,0] ] ) = true
11

Use underscore to determine the difference between the two, and check the length of the array. An easy way to do this would be:

_.isEmpty(_.difference(array1, array2)) && _.isEmpty(_.difference(array2, array1))

This will return true if they are the same and false if they are not.

2 Comments

.isEmpty(.difference([], [1,2,3])) returns true but the array are not the same. It should be .isEmpty(.difference(array1, array2)) && .isEmpty(.difference(array2, array1))
Well spotted. Stackoverflow removes the underscores in the comments for some reason
4
_.isEmpty(_.xor(array1, array2))

Comments

2

My implementation with http://underscorejs.org/

/**
 * Returns true if the arrays are equal
 *
 * @param {Array} array1
 * @param {Array} array2
 * @returns {boolean}
 */
equal: function ( array1, array2 )
{
    return ( array1.length === array2.length)
        && (array1.length === _.intersection( array1, array2 ).length);
}

Comments

1

If you want to check that the elements are the same and in the same order, I would go with:

arrayEq = function(a, b) {
  return _.all(_.zip(a, b), function(x) {
    return x[0] === x[1];
  });
};

Even more neatly in coffeescript:

arrayEq = (a,b) ->
    _.all _.zip(a,b), (x) -> x[0]==x[1]

2 Comments

or for every set of value pairs, return _.union(x).length == 1; for arbitrary numbers of arrays compared.
When order matters, you can just use _.isEqual.
0

If you don't need to know which elements are unequal, use transitivity:

function allEqual(list) {
  return _.all(list.slice(1), _.partial(_.isEqual, list[0]));
}
allEqual([2, 2, 2, 2]) //=> true
allEqual([2, 2, 3, 2]) //=> false
allEqual([false])      //=> true
allEqual([])           //=> true

Comments

0

I can't comment on Dan Tao's answer, small change to skip checking of first array against itself (unnecessary _.difference call)

function allArraysAlike(arrays) {
    return _.all(arrays, function(array) {
        if(array === arrays[0]) return true;
        return array.length == arrays[0].length && _.difference(array, arrays[0]).length == 0;
    });
}

3 Comments

Afaik, you can't compare 2 arrays with === eg, [4,5] === [4,5] will always be false! At least in chrome.
back then when I wrote this comment I was working on big and messy web app, with various stores/models etc.. there were arrays laying around representing same data (ids etc), some of arrays were copies, some direct references and we have to check them for differences. Sometimes we'd check same arrays against each others (in case they were references), this check here only optimizes this case: a = [1, ... x]; b = a; allArraysAlike([a, b])
it appears that "all" is now deprecated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.