2015-12-06 135 views
-3

我向上帝發誓這工作,我知道這是因爲我的測試傳遞了一段時間......現在它不。請參閱下面的錯誤屏幕截圖。For循環ES6不再工作

describe('',() => { 

    let newBoard; 

    beforeEach(() => { 
     newBoard = new board(); 
    }); 

    it('',() => { 
     for(let row in newBoard){ 
      for(let column in row){ 
       newBoard[row][column].should.equal(""); 
      } 
     } 
    }); 
}); 

board.js

module.exports = function(){ 
    return [["", "", ""], 
      ["", "", ""], 
      ["", "", ""]]; 
}; 

enter image description here

+0

什麼是'newBoard'嗎? –

+6

這不可能像預期的那樣工作......第二個循環應該是for(讓列在newBoard [row]中)。 (或使用for-of循環)。 –

+0

@LayTaylor它在屏幕截圖中說:'newBoard:Array [3]' – Stijn

回答

0

雖然通過@RobW註釋應該問題:for (let column in newBoard[row]).

我想補充一個建議​​:既然您使用ES6你可能想使用for of循環:

it('',() => { 
    for(let row of newBoard){ 
     for(let cell of row){ 
      cell.should.equal(""); 
     } 
    } 
}); 
-1

在你的內部循環你問它來創建一個變量column每個項目的對象是一個數字。 for in構造函數爲您提供newBoard陣列中每個項目的索引。

內部循環需求newBoard給定的索引來訪問陣列,使你的代碼變得for(let column in newBoard[row])

0

一些看家項目第一。你有一個for-in循環,而不是for循環。 For-in循環對於學習JavaScript的人來說是一個頻繁的錯誤來源,因爲它看起來像是做你想做的事情,但它不適用於迭代數組。它的意思是迭代對象的屬性。

您正在尋找的新ES6循環稱爲for-of loop。它的工作是迭代數組中的項目。在你的例子中,嘗試改變你的代碼來使用for-for循環。

it('',() => { 
    for(let row of newBoard){ 
     for(let column of row){ 
      column.should.equal(""); 
     } 
    } 
}); 
+0

代碼錯了 – CoderPi

+0

我試過了而對於 – PositiveGuy

+0

它應該是'for(let cell of row){cell.should.equal(「」); }' – CoderPi