How to append to an object with JavaScript?

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.