2012-04-21 95 views
1

我想一個函數中返回數組是這樣的:的JavaScript返回一個二維數組中的函數

function loadMap(map) { 
    if (map == 1) { 
     return board = [][ 
[ 1, 1, 1, 1, 1, 1, 1, 190, 115, 1, 1, 1, 1, 1, 1, 2], 
[ 190, 190, 190, 190, 190, 190, 190, 190, 13, 148, 148, 148, 148, 148, 121, 2], 
[ 1, 520, 127, 127, 127, 127, 127, 13, 13, 148, 167, 167, 167, 148, 343, 1], 
[ 1, 520, 127, 166, 166, 166, 127, 13, 13, 148, 167, 167, 167, 148, 343, 1], 
[ 1, 520, 127, 166, 166, 166, 127, 13, 13, 148, 148, 148, 183, 148, 343, 1], 
[ 1, 520, 364, 174, 127, 361, 127, 13, 13, 13, 13, 13, 13, 13, 13, 1], 
[ 115, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 115], 
[ 1, 514, 13, 13, 394, 343, 145, 220, 145, 145, 145, 13, 13, 13, 13, 1], 
[ 1, 514, 13, 13, 343, 118, 145, 166, 166, 166, 145, 13, 13, 13, 13, 1], 
[ 1, 514, 514, 13, 118, 118, 145, 166, 166, 166, 145, 13, 13, 13, 13, 1], 
[ 1, 1, 1, 115, 1, 1, 145, 145, 145, 145, 145, 1, 1, 1, 1, 1] 
]; 
    } 
} 

但是這是不對的。我該如何解決?

回答

5

你不給的返回值的名稱與功能,返回值,當你把它分配給一個變量。

在數組文字前面還有一個額外的[]

function loadMap(map) { 
    if (map == 1) { 
    return [ 
     [ ... ], 
     [ ... ], 
     [ ... ], 
     ... 
    ]; 
    } 
} 

var board = loadMap(1); 
0
function loadMap(map) { 
    if (map == 1) { 
     return [ 
      [values], 
      [values], 
      [values] 
     ] 
    } 
} 

你可以像map[x][y]訪問它們:

var map = loadMap(1); 

map[1][1]; //190 
相關問題