Categories
JavaScript Answers

How to push object into array with JavaScript?

Spread the love

Sometimes, we want to push object into array with JavaScript.

In this article, we’ll look at how to push object into array with JavaScript.

How to push object into array with JavaScript?

To push object into array with JavaScript, we use the push method.

For instance, we write

const vals = [];
const obj = {};
obj["01"] = n.label;
obj["02"] = n.value;
vals.push(obj);

to call vals.push to append obj as the last entry of the vals array.

We use

const obj = {};
obj["01"] = n.label;
obj["02"] = n.value;

to add the entries.

Conclusion

To push object into array with JavaScript, we use the push method.

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 *