2017-03-01 68 views
0

如何獲得在Javascript如何獲得在二維數組單元格的值在Javascript

在二維數組單元格的值我有一個數字表,將31周的cols * 9行,我想要得到的細胞獲得現場功能的價值!所以我想要[1] [2]。對我來說重要的價值只有Y?如何將網格傳遞給點函數?此外,對於性能有任何建議將是非常美妙的,你可以看到的是巨大的數組

var cols = 31; 
    var rows = 9; 
    var theGrid = new Array(cols); 
    var i; 
    var j; 
    //get the spot 
    function getTheSpot(j, i) { 
     // when the col met the row it create a spot 
     //that what i need to get 
     this.y = i; 
     this.x = j; 
     return i; 
     return j; 
    } 

    //create a grid for the numbers 
    function createGrid() { 
     // BELOW CREATES THE 2D ARRAY 
     for (var i = 0; i < cols; i++) { 
      theGrid[i] = new Array(rows); 
     } 

     for (var i = 0; i < cols; i++) { 

      for (var j = 0; j < rows; j++) { 
       theGrid[j][i] = new getTheSpot(j, i); 

      } 
     } 

    } 
    var s = getTheSpot(9, 2); 
    console.log (s); 
+1

你介意給我們展示你到目前爲止嘗試過什麼嗎? –

回答

0

如果我知道你需要什麼正確的,你可以參考這樣的數組元素:

var your_array = [ 10, 11, 15, 8 ]; 

// Array indexes start at 0 
// array[0] is the the first element 
your_array_name[0] == 10 
//=> true 

// array[2] is the third element 
your_array_name[2] == 15 
//=> true 

現在,二維矩陣(一個陣列內的陣列),這裏的東西怎麼走:

var awesome_array = [ 
    [ 0, 10, 15, 8 ], 
    [ 7, 21, 75, 9 ], 
    [ 5, 11, 88, 0 ] 
]; 

// Remember the index starts at 0! 

// First array, first element 
awesome_array[0][0] == 0 
//=> true 

// Second array, fourth element 
awesome_array[1][3] == 9 
//=> true 

在你的情況,你(據說)有這樣的佈局:

var greatest_array = [ 
    [ "A", "B", "C", "D", "E", "F" ], 
    [ "B", "C", "D", "E", "F", "G" ], 
    [ "C", "D", "E", "F", "G", "H" ] 
]; 

// Your desired "E" is on the second array (index 1), fourth line (index 3): 
console.log(greatest_array[1][3]); //=> "E" 

乾杯!

相關問題