How to Remove Element from 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,

If you need to see lesson of how to remove element from array in nodejs. we will help you to give lesson of node js remove object from array. i explained simply about node js remove element from array. We will look at lesson of how to remove object from array in node js.

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

Lesson 1: Node Remove Element from Array by Index

server.js

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

var index = 2;

myArray.splice(index, 1);

console.log(myArray);

Result:-

[ 1, 2, 4, 5 ]

Lesson 2: Node Remove Element from Array by Value

server.js

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

var value = 4;

myArray.splice(myArray.indexOf(), 1);

console.log(myArray);

Result:-

[ 1, 2, 4, 5 ]

Lesson 3: Node Delete Item from Array by Object

server.js

myArray = [

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

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

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

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

];

id = 2;

myArray = myArray.filter(item => item.id !== id);

console.log(myArray);

Result:-

[

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

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

{ id: 3, name: 'Mahesh' }

]

thank you very much for following up with me.

Tags

Recent Posts

Related Posts