How to Remove Element from Array in Node JS?

By Hardik Savani May 26, 2021 Category : Node JS

Hi,

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

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

Example 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);

Output:

[ 1, 2, 4, 5 ]

Example 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);

Output:

[ 1, 2, 4, 5 ]

Example 3: Node Delete Item from Array by Object

server.js

myArray = [

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

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

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

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

];

id = 2;

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

console.log(myArray);

Output:

[

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

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

{ id: 3, name: 'Mahesh' }

]

i hope it can help you...

Tags :
Shares