Categories
JavaScript Answers

How to append to an object with JavaScript?

Spread the love

Sometimes, we want to append to an object with JavaScript.

in this article, we’ll look at how to append to an object with JavaScript.

How to append to an object with JavaScript?

To append to an object with JavaScript, we can use the spread operator.

For instance, we write

let alerts = {
  1: { app: "helloworld", message: "message" },
  2: { app: "helloagain", message: "another message" },
};

alerts = {
  ...alerts,
  alertNo: 2,
};

to spread the existing properties of alerts into a new object with ....

And then we add the alertNo property to it.

Then we assign the new object back to alerts.

Conclusion

To append to an object with JavaScript, we can use the spread operator.

By John Au-Yeung

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

Leave a Reply

Your email address will not be published. Required fields are marked *