How To Change A Property In An Array Of Objects

Timo Müller

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?

Mohammad Usman

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I change the name property of the objects in my mutable array?

From Dev

Use underscore to change one property of objects in an array

From Dev

How to access property of array of objects

From Dev

how to access objects in an array by a property of those objects?

From Dev

How to filter by an array property in an array of objects

From Dev

How to change property of other objects in QML

From Dev

How to remove array objects having property

From Dev

How to remove array objects having property

From Dev

How to search by a property in array of objects in php?

From Dev

How to sort array of objects according to a property of the object

From Dev

Understanding objects in a array and how the length property works on objects

From Dev

How to change a JSON string to an array of objects?

From Java

How to find multiple indexes in array of objects by the value of an object property that nests in a property of array of objects?

From Dev

Sort array with objects on property of objects

From Dev

Sort array with objects on property of objects

From Dev

How to assign an array of int to a corresponding property of an array of objects in Swift?

From Dev

Sorting array of objects by property

From Java

Add property to an array of objects

From Dev

Summing objects property in array

From Dev

Group objects in array by property

From Dev

How to propagate property change notifications of objects within collections

From Dev

Array of buttons: change property

From Java

How to compare and filter objects of array based on property value?

From Dev

How to access an object property from an array of objects in a click handler?

From Dev

How to filter array of objects by element property values using jq?

From Java

Swift how to sort array of custom objects by property value

From Java

How to group array of objects by value of object and sum the result in new property

From Java

How can I check if the array of objects have duplicate property values?

From Dev

How to remove a specific Object from an array of Objects, by object's property?

Related Related

  1. 1

    How can I change the name property of the objects in my mutable array?

  2. 2

    Use underscore to change one property of objects in an array

  3. 3

    How to access property of array of objects

  4. 4

    how to access objects in an array by a property of those objects?

  5. 5

    How to filter by an array property in an array of objects

  6. 6

    How to change property of other objects in QML

  7. 7

    How to remove array objects having property

  8. 8

    How to remove array objects having property

  9. 9

    How to search by a property in array of objects in php?

  10. 10

    How to sort array of objects according to a property of the object

  11. 11

    Understanding objects in a array and how the length property works on objects

  12. 12

    How to change a JSON string to an array of objects?

  13. 13

    How to find multiple indexes in array of objects by the value of an object property that nests in a property of array of objects?

  14. 14

    Sort array with objects on property of objects

  15. 15

    Sort array with objects on property of objects

  16. 16

    How to assign an array of int to a corresponding property of an array of objects in Swift?

  17. 17

    Sorting array of objects by property

  18. 18

    Add property to an array of objects

  19. 19

    Summing objects property in array

  20. 20

    Group objects in array by property

  21. 21

    How to propagate property change notifications of objects within collections

  22. 22

    Array of buttons: change property

  23. 23

    How to compare and filter objects of array based on property value?

  24. 24

    How to access an object property from an array of objects in a click handler?

  25. 25

    How to filter array of objects by element property values using jq?

  26. 26

    Swift how to sort array of custom objects by property value

  27. 27

    How to group array of objects by value of object and sum the result in new property

  28. 28

    How can I check if the array of objects have duplicate property values?

  29. 29

    How to remove a specific Object from an array of Objects, by object's property?

HotTag

Archive