简体   繁体   中英

Comparing Arrays

I have a little piece of code where an array is populated with arrays. At the end, I have this array with n member arrays. These sub-arrays always have the same number of members (2) of the same type (number). I need to know if these sub-arrays are all identical (same members, same position). This always returns false:

[2, 0] === [2, 0]

Off the top of my head, I can think of two ways to compare.

  • A loop in which I test (member[i][0] === member[i+1][0] && member[i][1] === member[i+1][1]).
  • The same loop, with this test: (member[i].toString() === member[i + 1].toString()).

I think I'm taking this the wrong way. What do you think?

FOR MY OWN THINKING, I think it is correct to use the loop to compare two array.

var isTheSame = true;
for (var i in array1) {
    if (array1[i] !== array2[i]) isTheSame = false;
}

The loop above works, it will return false either any of the element different from type (three equal sign ===), or value, or length or even key.

The second suggestion of you should not work as exactly as the first one, because you convert it into a string already, what happen if array1[0] = "true" and array2[0] = true? It will return true, because now all of them are string, but you need the exact comparison right?

That's my own thinking, I hope it might help somehow.

Regards, [x]

a=[2,0]; b=[2,0]; a.toString() == b.toString();

perhaps not the most efficient, but it seems to work, and I'm a strong proponent of the shorter and more readable solution.

note that xx3004's point about the type data lost when converting to string is something to think about, unless you know for sure that your arrays will be composed of the same data types.

You can use the below to get true or false on whether two one-dimensional arrays are identical. It needs to be recursive if you add dimensions, but this will suffice for what you're asking, I think.

function testArray(a, b) {
    var aLen = a.length;
    var bLen = b.length;

    if (aLen === bLen) { // check for identical length first
        for (var i = 0; i < aLen; i++) {
            if (a[i] !== b[i]) {
                return false; // members don't match
            }
        }
        return true; // all members matched
    }
    return false; // not same length
}

http://jsfiddle.net/pgkUr/

This is what I came up with...

var arrayCompare = function(a, b) {

   var aLength = a.length,
       bLength = b.length;

   if (aLength != bLength) {
      return false;
   }

   for (var i = 0; i < aLength; i++) {
      if (a[i] !== b[i]) {
         return false;
      }
   }

   return true;

}

Here's an easy way to compare the two arrays, building on your toString idea (even multidimensional ones will work):

function compareArrays(a1, a2) {
    return JSON.stringify(a1) == JSON.stringify(a2);
}

compareArrays([[2,3],[7,4],[3,3]], [[2,3],[7,4],[3,3]]) // true
compareArrays([[2,3],[7,4],[3,3]], [[7,4],[2,3],[3,3]]) // false

If your willing to use Underscore , which is simply great tool (I value it as much or even higher than jQuery), you could simplify this code quite a lot. You don't need nesting during comparing arrays: I would flatten them beforehand. Then it's just one loop:

function cmpArrays( arr1, arr2 ){
    var flat = _.zip( _.flatten( arr1 ), _.flatten( arr2 ) );

    for( var elem in flat ){
        if( flat[elem][0] !== flat[elem][1] ){
            return false;
        }
    }

    return true;
}

generalizing this to work with any number of arrays should be very simple as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM