Timo Müller
Timo Müller

Reputation: 5

How To Change A Property In An Array Of Objects

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?

Upvotes: 0

Views: 118

Answers (5)

Faly
Faly

Reputation: 13356

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

Upvotes: 0

Manish Vadher
Manish Vadher

Reputation: 1546

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

Upvotes: 1

Jonas Wilms
Jonas Wilms

Reputation: 138267

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.

Upvotes: 0

felixmosh
felixmosh

Reputation: 35503

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)

Upvotes: 1

Mohammad Usman
Mohammad Usman

Reputation: 39322

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

Upvotes: 2

Related Questions