2017-08-11 136 views
-1

我想比較兩個數組是否相等。我打破了數組a,並最終將它們存儲到兩個不同的陣列bc。最後,我正在控制檯中檢查數組bc如何檢查兩個數組是否相等?

控制檯顯示相同的值,但是當我比較兩個數組時,我得到的數組並不相等。

這裏是我的代碼:

var a = [1,2,3,4,3,2,1]; 
var b = []; 
var c = []; 
var t = 0; 
var length = a.length; 

console.log("is the array length" + length); 
if (length %2 !== 0) { 
    var mid = parseInt(length/2)-1; 
    console.log(a[mid]); 
    for(var j=length-1; j>(mid+1); j--) { 
     c[t] = a[j]; 
     t++; 
    } 
    for(var i=0; i<=mid; i++) { 
     b[i] = a[i]; 
    } 
    console.log(c); 
    console.log(b); 

    if(b == c) { //comparing the array b and c 
     console.log("true"); 
    } 
    else { 
     console.log("no") 
    } 
} 

這裏是我的jsbin鏈接:https://jsbin.com/metexuruka/edit

+1

數組是永遠相等。它們存儲在不同的MEM位置。 –

+0

所以我不能比較兩個數組? –

回答

1

首先,您的代碼拆分數組過於複雜。你可以簡單地切:

var a= [1,2,3,4,3,2,1], 
mid = Math.floor(a.length/2), 
b = a.slice(0,mid), 
c = a.slice(mid).reverse(); 

要比較兩個數組,你可以創建一個字符串(你可以很容易地比較字符串):

if(b.join() === c.join()) alert("equal"); 

或者你迭代並檢查每個:

if(b.length === c.length 
    && b.every((v,i)=> v === c[i])) alert("equal"); 

如果你只是想比較一下,如果是一個annagramm,它更容易:

var a= [1,2,3,4,3,2,1]; 

if(a.every((v,i)=>v===a[a.length-1-i])) alert("anagram!"); 
+0

我想以相反的方式使用數組C,所以我使用for循環以相反的順序得到它......因爲我無法使用切片方法執行此操作。 –

+0

@ashish sah,那麼.reverse()對...有好處... –

2

取決於你的「平等」的定義 - 如果它是兩個數組包含相同的元素在同一位置,您可以使用every

let areEqual = a.length === b.length && a.every((item, index) => b[index] === item); 

如果你只是想檢查它們包含相同的元素,您仍然可以使用every,菊st沒有索引檢查:

let areEqual = a.length === b.length && a.every(item => b.indexOf(item) > -1); 
0

它太長了,但你可以使用這個:

<script type="text/javascript"> 
 
     /*$(function() { 
 
      $(".nav-item").click(function() { 
 
       $(".nav-item").each(function() { 
 
        $(this).find("a").removeClass("active"); 
 
       }); 
 
       $(this).find("a").addClass("active"); 
 
      }); 
 
     });*/ 
 

 
     // Warn if overriding existing method 
 
     if (Array.prototype.equals) 
 
      console.warn("Overriding existing Array.prototype.equals. Possible causes: New API defines the method, there's a framework conflict or you've got double inclusions in your code."); 
 
     // attach the .equals method to Array's prototype to call it on any array 
 
     Array.prototype.equals = function (array) { 
 
      // if the other array is a falsy value, return 
 
      if (!array) 
 
       return false; 
 

 
      // compare lengths - can save a lot of time 
 
      if (this.length != array.length) 
 
       return false; 
 

 
      for (var i = 0, l = this.length; i < l; i++) { 
 
       // Check if we have nested arrays 
 
       if (this[i] instanceof Array && array[i] instanceof Array) { 
 
        // recurse into the nested arrays 
 
        if (!this[i].equals(array[i])) 
 
         return false; 
 
       } 
 
       else if (this[i] != array[i]) { 
 
        // Warning - two different object instances will never be equal: {x:20} != {x:20} 
 
        return false; 
 
       } 
 
      } 
 
      return true; 
 
     } 
 
     // Hide method from for-in loops 
 
     Object.defineProperty(Array.prototype, "equals", { enumerable: false }); 
 

 
     var a = [1, 2, 3, 4, 3, 2, 1]; 
 
     var b = []; var c = []; var t = 0; 
 
     var length = a.length; 
 
     alert("is the array length" + length); 
 
     if (length % 2 !== 0) { 
 
      var mid = parseInt(length/2) - 1; 
 
      alert(a[mid]); 
 
      for (var j = length - 1; j > (mid + 1) ; j--) { 
 
       c[t] = a[j]; 
 
       t++; 
 
      } 
 
      for (var i = 0; i <= mid; i++) { 
 
       b[i] = a[i]; 
 
      } 
 
      alert(c); 
 
      alert(b); 
 

 
      if (b.equals(c)) { //comparing the array b and c 
 
       alert("true"); 
 
      } 
 
      else 
 
       alert("no"); 
 
     } 
 
    </script>

相關問題