I have an array of 800 objects.
cells = [
{ x_position: 0, y_position: 0, terrain: 'water' },
{ x_position: 0, y_position: 1, terrain: 'water' },
{ x_position: 0, y_position: 2, terrain: 'water' },
{ x_position: 0, y_position: 3, terrain: 'water' },
{ x_position: 0, y_position: 4, terrain: 'water' },
...
]
Let's say for some x_position
and y_position
I want to change the terrain
to 'land'
.
How can I iterate through the array changing the terrain?
You can use forEach()
:
let cells = [
{ x_position: 0, y_position: 0, terrain: 'water' },
{ x_position: 0, y_position: 1, terrain: 'water' },
{ x_position: 0, y_position: 2, terrain: 'water' },
{ x_position: 0, y_position: 3, terrain: 'water' },
{ x_position: 0, y_position: 4, terrain: 'water' }
];
let match = {
x_position: 0,
y_position: 2
}
cells.forEach(o => {
if(o.x_position == match.x_position && o.y_position == match.y_position)
o.terrain = 'land';
});
console.log(cells);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments