Categories
JavaScript Answers

How to update a JSON object using JavaScript?

Spread the love

To update a JSON object using JavaScript, we can use a loop.

For instance, we write

for (const o of jsonObj) {
  if (o.id === 3) {
    o.username = "Thomas";
    break;
  }
}

to loop through the objects in the jsonObj array.

In the loop, we find the item with id 3.

If it’s true, then we set its username property value to 'Thomas'.

Conclusion

To update a JSON object using JavaScript, we can use a loop.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.