【发布时间】:2020-03-18 16:10:36
【问题描述】:
我正在为学校创建一个小游戏,我正在创建一个由 100 个图块组成的棋盘,它们是 JS 对象。其中一些瓷砖被随机阻挡(这是它们的属性之一)。玩家不能进去。
我还需要在这些图块上随机放置 4 把武器。
所以我创建了一系列未阻挡的图块,然后我将我的武器一个一个地放在这些空闲的图块上。为了确保其中一个图块上没有武器,我使用 .filter() 创建了一个新的未阻塞且空的图块数组(没有武器)。
问题是,当我在我的新数组中更新一个图块的属性时(发出 .filter,以确保不会有两个武器在同一个位置),它会更新图块的属性父数组,我不明白这一点。我读过 .filter() 方法不更新父数组,但在这里它可以吗? 也许是因为它是一个 JS 对象?
感谢您的澄清!
let tilesUnblocked = []
//here function creating tiles and pushing unblocked tiles in the array above
for (i = 0; i < weapons.length; i++) {
let tilesUnblockedAndEmpty = tilesUnblocked.filter(tile => tile.weapon === false)
let randomInt = Math.round(tilesUnblockedAndEmpty.length * Math.random())
let weaponElt = weapons[i].HTMLElement
weaponElt.css({ //some css })
tilesUnblockedAndEmpty[randomInt].HTMLElement.append(weaponElt)
tilesUnblockedAndEmpty[randomInt].weapon = true //updating array created from filter
tilesUnblockedAndEmpty[randomInt].weaponType = weapons[i].name //updating array created from filter
}
console.log(tilesUnblocked) //I will find in this array the objects updated at the end of the foor loop, on the array issued of .filter method !
【问题讨论】:
-
.filter()创建一个新数组,但不是所有对象的新副本。这两个数组共享对相同对象的引用。 -
在 filter func try .map(item => {...item}) 之后,这将创建新对象并用相同的对象但不同的引用填充新数组
标签: javascript arrays object for-loop filter