Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>On the same lines as JSON.encode is to use join().</p> <pre><code>function checkArrays( arrA, arrB ){ //check if lengths are different if(arrA.length !== arrB.length) return false; //slice so we do not effect the original //sort makes sure they are in order //join makes it a string so we can do a string compare var cA = arrA.slice().sort().join(","); var cB = arrB.slice().sort().join(","); return cA===cB; } var a = [1,2,3,4,5]; var b = [5,4,3,2,1]; var c = [1,2,3,4]; var d = [1,2,3,4,6]; var e = ["1","2","3","4","5"]; //will return true console.log( checkArrays(a,b) ); //true console.log( checkArrays(a,c) ); //false console.log( checkArrays(a,d) ); //false console.log( checkArrays(a,e) ); //true </code></pre> <p>Only problem is if you care about types which the last comparison tests. If you care about types, you will have to loop.</p> <pre><code>function checkArrays( arrA, arrB ){ //check if lengths are different if(arrA.length !== arrB.length) return false; //slice so we do not effect the orginal //sort makes sure they are in order var cA = arrA.slice().sort(); var cB = arrB.slice().sort(); for(var i=0;i&lt;cA.length;i++){ if(cA[i]!==cB[i]) return false; } return true; } var a = [1,2,3,4,5]; var b = [5,4,3,2,1]; var c = [1,2,3,4]; var d = [1,2,3,4,6]; var e = ["1","2","3","4","5"]; console.log( checkArrays(a,b) ); //true console.log( checkArrays(a,c) ); //false console.log( checkArrays(a,d) ); //false console.log( checkArrays(a,e) ); //false </code></pre> <p>If the order should remain the same, than it is just a loop, no sort is needed.</p> <pre><code>function checkArrays( arrA, arrB ){ //check if lengths are different if(arrA.length !== arrB.length) return false; for(var i=0;i&lt;arrA.length;i++){ if(arrA[i]!==arrB[i]) return false; } return true; } var a = [1,2,3,4,5]; var b = [5,4,3,2,1]; var c = [1,2,3,4]; var d = [1,2,3,4,6]; var e = ["1","2","3","4","5"]; console.log( checkArrays(a,a) ); //true console.log( checkArrays(a,b) ); //false console.log( checkArrays(a,c) ); //false console.log( checkArrays(a,d) ); //false console.log( checkArrays(a,e) ); //false </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload