简体   繁体   中英

javascript, compare arrays of different sizes

with two arrays of potentially different sizes, what is best way to see if they are the same as far as it goes

for example

var a1 = [ 1, 2, 3 ];
var a2 = [ 1, 2 ];
var a3 = [ 1, 3 ];

a1 == a2 => true;
a1 == a3 => false;

am positive this has been done thousands of times and the syntax is well memorized

What about this (I'll just demonstrate on a1 and a2 -> I presume you can make function out of this):

var min_val = min(a1.length, a2.length);
var equals = true;

for(i = 0; i < min_val; i++)
{
    if(a1[i] != a2[i])
    {
        equals = false;
        break;
    }
}

The result will be in equals variable of course. If you want to make function out of this, just pass a1 and a2 as arguments and return equals.

function compareArraySeq(a1, a2) {
  var i, l = Math.min(a1.length, a2.length); 

  for (i=0; i<l; i++) {
    if (a1[i] !== a2[i]) return false;
  }
  return true;
}

[ ] based on Tomalaks comments I'd say JSON can come to the rescue. ]根据Tomalaks评论,我会说JSON可以来救援。

So, again: here's an Array extension that does what [I suppose] you want to do:

function comparePartial(arr1,arr2){
  var arr2 = this, l1 = arr1.length, l2 = arr2.length;

  return ( l1<1 || l2<1
            ? false :
              JSON.stringify(arr1.slice(0, l2)) ===
              JSON.stringify(arr2.slice(0, l1))
         );
}
Array.prototype.comparePartial = 
    Array.prototype.comparePartial || comparePartial;

//usage
    var a1 = [ 1, 2, 3 ]
   ,a2 = [ 1, 2 ]
   ,a3 = [ 1, 3 ]
   ,a4 = ['','']
   ,a5 = ['','','']
   ,a6 = []
   ,a7 = ['bla','doh',1]
   ,a8 = ['bla','doh',1,'yeah','really']
   ,a9 = [1,3,5,'doh']
   ,a10= ['1','3','5','doh']
   ,a11= [{a:1,b:2},{c:3,d:4}]
   ,a12= [{a:1,b:2},{c:3,d:4},{e:5,f:6}]

console.log(
  [ a1.comparePartial(a2)
   ,a2.comparePartial(a1)
   ,a1.comparePartial(a3)
   ,a4.comparePartial(a5)
   ,a5.comparePartial(a6)
   ,a1.comparePartial(a6)
   ,a8.comparePartial(a7)
   ,a10.comparePartial(a9)  //=> 'type safe' comparison
   ,a11.comparePartial(a12) //=> can compare arrays of Objects
  ].join(' - ')
); //=> true - true - false - true - false - false - true - false - true
function compareArraySeq(a, b) {
    return a.slice(0, b.length).join(' ') == b.slice(0, a.length).join(' ');
}
function prefixEqual(a, b) {
    var prefixLength = a.length < b.length ? a.length : b.length;
    for(var i = 0; i < prefixLength; i+=1)
        if( a[i] != b[i] )
            return false;
    return true;
}

Make a loop checking one spot at a time. I have made this:

var compare = function (a1, a2) {
    var l = Math.min(a1.length, a2.length);
    for (var i = 0; i < l; i++) {
        if (a1[i] !== a2[i]) {
            return false;
        }
    }
    return true;
}

Now you can compare arrays like this:

var a = [0, 1, 2, 3];
var b = [0, 1, 2];
var c = [0, 1, 3];

compare(a, b); //true
compare(a, c); //false

Hope this works for you :)

Fiddle link: http://jsfiddle.net/8zbJj/1/

If your arrays are strings or numbers or booleans you can compare their String values.

function compareSimpleValues(a,b){
  if(a.length>=b.length)return String(a).indexOf(String(b))===0;
  return String(b).indexOf(String(a))===0;
}

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