最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript, compare arrays of different sizes - Stack Overflow

matteradmin9PV0评论

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

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

Share Improve this question asked Feb 12, 2012 at 12:08 cc youngcc young 20.2k32 gold badges94 silver badges150 bronze badges 2
  • Why is the first true and the second false? – Gumbo Commented Feb 12, 2012 at 12:10
  • why al & a2 are same?? because a2 content is there in a1?? – Fahim Parkar Commented Feb 12, 2012 at 12:10
Add a ment  | 

7 Answers 7

Reset to default 4

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 pareArraySeq(a, b) {
    return a.slice(0, b.length).join(' ') == b.slice(0, a.length).join(' ');
}
function pareArraySeq(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;
}

[edit] based on Tomalaks ments I'd say JSON can e to the rescue.

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

function parePartial(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.parePartial = 
    Array.prototype.parePartial || parePartial;

//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.parePartial(a2)
   ,a2.parePartial(a1)
   ,a1.parePartial(a3)
   ,a4.parePartial(a5)
   ,a5.parePartial(a6)
   ,a1.parePartial(a6)
   ,a8.parePartial(a7)
   ,a10.parePartial(a9)  //=> 'type safe' parison
   ,a11.parePartial(a12) //=> can pare arrays of Objects
  ].join(' - ')
); //=> true - true - false - true - false - false - true - false - true
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 pare = 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 pare arrays like this:

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

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

Hope this works for you :)

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

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

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far