How to Push Element in Array in Node JS?

About Us:

We are from free coder. We are a group of web developers who are passionate about web development. You can learn from us php, laravel, node js, vue js, react js and many other topic on web developement.
we are very happy to help you.
free coder

Today what you are going to learn:

Hello Brother,

This tutorial shows you how to push element in array in node js. i explained simply about how to push object in array in node js. In this article, we will implement a how to add json object to array in node js. i would like to show you node js add object to array. So, let's follow few step to create lesson of node js add value to array.

Hello Brother, i will give you three simple lesson to adding key value in array with node. so, let's see bellow lesson how to push object in array in node js app.

Lesson 1:

server.js

myArray = [1, 2, 3, 4, 5, 6];

myArray.push(7);

console.log(myArray);

Result:-

[

1, 2, 3, 4,

5, 6, 7

]

Lesson 2:

server.js

myObjArray = [

{id: 1, name: "Sagor" },

{id: 2, name: "Vimal" },

{id: 3, name: "Paresh" }

];

myObjArray.push({id: 4, name: "Karan"});

console.log(myObjArray);

Result:-

[

{ id: 1, name: 'Sagor' },

{ id: 2, name: 'Vimal' },

{ id: 3, name: 'Paresh' },

{ id: 4, name: 'Karan' }

]

Lesson 3: Add on Top

server.js

myObjArray = [

{id: 1, name: "Sagor" },

{id: 2, name: "Vimal" },

{id: 3, name: "Paresh" }

];

myObjArray.unshift({id: 4, name: "Karan"});

console.log(myObjArray);

Result:-

[

{ id: 4, name: 'Karan' },

{ id: 1, name: 'Sagor' },

{ id: 2, name: 'Vimal' },

{ id: 3, name: 'Paresh' }

]

thank you very much for following up with me.

Tags

Recent Posts

Related Posts