Change a Value of an Object in an Array in JavaScript-Code Examples

Radu TM

Change a Value of an Object in an Array in JavaScript

JavaScript by Radu TM June 17, 2022

var fruits = [
  {name: "🍎", color: "red"},
  {name: "🍊", color: "orange"},
  {name: "🍋", color: "yellow"},
  {name: "🍌", color: "green"},
  {name: "🍉", color: "pink"},
  {name: "🍇", color: "purple"},
];

//change the color of the first fruit to blue
fruits[0].color = "blue";

//change the color of the last fruit to brown
fruits[fruits.length - 1].color = "brown";

0

17.147

Change a Value of an Object in an Array in JavaScript

JavaScript by Radu TM June 17, 2022

var myArray = [
  {
    "name": "Sarah",
    "age": "23"
  },
  {
    "name": "John",
    "age": "27"
  }
]

//change John's age to 30
myArray[1].age = 30;

0

17.147

Related JavaScript Queries

Latest JavaScript Queries

Explore code examples by categories