Freemote Logo

Coding Wiki

Most Common Coding Questions

Find answers to popular programming questions and level up your coding skills

Array js add object to array

Learn how to add an object to an array in JavaScript.

Answer:

In JavaScript, you can add an object to an array using the push() method or by spreading it into the array literal.

javascript
Array.prototype.push(myObject);
Array.from([1,2,3], item => ({...item, value: 'newValue'}))

Alternatively, you can use the spread operator to add a new object to an existing array.

javascript
const arr = [1,2,3];
arr.push(...[4,5,6]);

Note that the spread operator only works with arrays and objects that support the spread syntax.

Tags:
javascript
☾☾☾
+++++++++
Level Up Your Coding Skills

JavaScript Cheat Sheet

Remember the essentials of 5 different languages, it's free.