gpt4 book ai didi

JavaScript 在函数中返回二维数组

转载 作者:行者123 更新时间:2023-11-30 08:59:44 26 4
gpt4 key购买 nike

我试图在这样的函数中返回一个数组:

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]
];
}
}

然而这是不对的。我该如何解决这个问题?

最佳答案

在函数中不给返回值起名字,调用的时候把返回值赋值给一个变量。

你在数组文字前面有一个额外的 []

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

var board = loadMap(1);

关于JavaScript 在函数中返回二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10255607/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com