0

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?

0

5 Answers 5

2

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);

Sign up to request clarification or add additional context in comments.

Comments

1

You can use Array.prototype.map to iterate over the array and change the object's terrain value;

Pay attention that my solution is immutable, meaning, it not changes the original array nor the objects.

const cells = [{
    x_position: 0,
    y_position: 0,
    terrain: 'water'
  },
  {
    x_position: 1,
    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'
  }
];

const result = cells.map((item) => {
  if (item.x_position === 1) {
    return { ...item,
      terrain: 'land'
    }
  } else {
    return item;
  }
});

console.log(result)

Comments

1

Simply Use this Code.

 var 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' },
      ];
       var i;
       for (i = 0; i < cells.length; i++) {
          cells[i].land = cells[i]['terrain'];
          delete cells[i].terrain;
       } console.log(cells);

2 Comments

Perfect Answer.
this is not a perfect answer ! you just misunderstood the question! op wants to change terrain value depending on x_position and y_position values
0

In the long term it might be good to build up a position hashmap:

 const byPosition = {};

 for(const cell of cells)
   byPosition[cell.position_x + "|" + cell.position_y] = cell;

So you can simply do:

byPosition["1|2"].terrain = "landscape";

If your cells are somehow forming a square you should definetly use a 2d array to store them.

Comments

0

You can find the element with array.prototype.find then change value of terain property to Land:

var 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' }
];
var x = 0, y = 2;
var item = cells.find(c => c.x_position === x && c.y_position === y);
item && item.terrain = 'land';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.