I'm creating a little game for school and I'm creating a board of 100 tiles, that are JS objects. Some of these tiles are blocked (it's one of their properties), randomly. Player can't go in it.
I need to put 4 weapons on these tiles, randomly too.
So I've created an array of tiles unblocked, and then I put my weapon one by one on these free tiles. To be sure that there is not already a weapon on one of these tiles, I use the .filter() to create a new array of tiles unblocked and empty (with no weapons).
The thing is, when I update the property of a tile in my new array (issued of .filter, to be sure that there won't be two weapons on the same spot), it updates the properties of the tile in the parent array, and I don't undestand that. I've read that .filter() method don't update parent array, but here it does ? Maybe it's because it's a JS object ?
Thanks for the clarification !
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 !