【发布时间】:2018-02-28 14:49:03
【问题描述】:
目前在我的应用程序中组合了一个函数,想知道是否有一种更简洁的方法来使用 ES6 编写此函数,而不是使用两个 for 循环。
目的是创建一个多维数组来跟踪坐标 x 和 y。这工作正常,但我希望让它更整洁。
function setBoard() {
boardParts = new Array(tileCount);
for (let i = 0; i < tileCount; ++i) {
boardParts[i] = new Array(tileCount);
for (let j = 0; j < tileCount; ++j) {
boardParts[i][j] = new Object();
boardParts[i][j].x = tileCount - 1 - i;
boardParts[i][j].y = tileCount - 1 - j;
}
}
emptyLoc.x = boardParts[tileCount - 1][tileCount - 1].x;
emptyLoc.y = boardParts[tileCount - 1][tileCount - 1].y;
solved = false;
}
感谢任何帮助!
谢谢
【问题讨论】:
-
即使在 ES5 中也有对象字面量 :-)
-
另外,
emptyLoc.x = emptyLoc.y = 0;- 那些总是一样的?
标签: javascript arrays function multidimensional-array ecmascript-6