1

Is there anyway to match two arrays to see if they are identical, including the order they are in. so [1,2,3,4] would match to [1,2,3,4] but not [1,3,2,4], [2,1,4,3] etc

user3328513
  • 189
  • 1
  • 3
  • 12

3 Answers3

2

Just try with:

var a = [1,2,3,4],
    b = [1,3,2,4],
    equals = a.length == b.length;

if (equals) {
  for (var i = 0; i < a.length; i++){
    if (a[i] !== b[i]) {
      equals = false;
      break;
    }
  }
}
hsz
  • 148,279
  • 62
  • 259
  • 315
2
var a  = [1,2,3,4];
var b = [1,3,2,4];
var c = [1,2,4,3];
var d = [1,2,3,4];

if(a.join('|') === b.join('|')) {
    console.log('Same');    
}
else {
    console.log('Not same');    
}
Gaurang Tandon
  • 6,504
  • 11
  • 47
  • 84
Suman Biswas
  • 853
  • 10
  • 19
0

try this logic, i hope it will help you

 var arr1 = [1,2,3,4];
 var arr2 = [1,2,4,4];
 var verify = true;

 for( var x = 0; x < arr1.length; x++ ){
    if( !(arr1[x] == arr2[x]) ){
       verify = false;
       break;
    }
 }

 if(verify){
   alert("match");
 }
 else{
    alert("not match");
 }
wrecklez
  • 343
  • 2
  • 4
  • This is actually not true. The OP wants to know whether two arrays are `identical`. Your code verifies that array `arr1` matches the `beginning` of array `arr2`. See @hsz's answer. – Tonio Jun 30 '14 at 11:15