简体   繁体   English

比较 Arrays

[英]Comparing Arrays

I have a little piece of code where an array is populated with arrays.我有一小段代码,其中一个数组填充了 arrays。 At the end, I have this array with n member arrays.最后,我得到了这个包含n 个成员 arrays 的数组。 These sub-arrays always have the same number of members (2) of the same type (number).这些子数组始终具有相同数量的相同类型(数量)的成员 (2)。 I need to know if these sub-arrays are all identical (same members, same position).我需要知道这些子数组是否都相同(相同的成员,相同的位置)。 This always returns false:这总是返回 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]).我测试的循环(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()).相同的循环,使用此测试:(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.上面的循环有效,它将返回 false 任何与类型(三个等号 ===)、值、长度甚至键不同的元素。

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?你的第二个建议不应该和第一个一样工作,因为你已经将它转换成一个字符串,如果 array1[0] = "true" 和 array2[0] = true 会发生什么? It will return true, because now all of them are string, but you need the exact comparison right?它将返回 true,因为现在它们都是字符串,但是您需要精确比较对吗?

That's my own thinking, I hope it might help somehow.这是我自己的想法,希望能有所帮助。

Regards, [x]问候,[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.请注意,xx3004 关于转换为字符串时丢失类型数据的观点是需要考虑的,除非您确定您的 arrays 将由相同的数据类型组成。

You can use the below to get true or false on whether two one-dimensional arrays are identical.您可以使用以下内容来判断两个一维 arrays 是否相同。 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/ 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):这是一个比较两个 arrays 的简单方法,基于您的toString想法(即使是多维的也可以):

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.如果你愿意使用Underscore ,这是一个很棒的工具(我认为它与 jQuery 一样甚至更高),你可以大大简化这段代码。 You don't need nesting during comparing arrays: I would flatten them beforehand.在比较 arrays 时不需要嵌套:我会事先将它们展平。 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.将其概括为与任意数量的 arrays 一起工作也应该非常简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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