You can push an object into an array in JavaScript by using the push()
method. Here is an example:
var fruits = [];
var apple = { type: 'apple', color: 'red' };
fruits.push(apple);
In this example, an object apple
is created, and then it is added to the fruits
array using the push()
method.
Note that push()
adds the element to the end of the array, so if you want to insert an element at a specific position in the array, you can use the splice()
method.
+ There are no comments
Add yours