ItSolutionStuff.com

How to Remove Element from Array in Node JS?

By Hardik Savani • May 26, 2021
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: Node JS
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to use Underscore JS in Node JS App?

Read Now →

Node JS Sort Array of Objects by Value Example

Read Now →

Node JS Foreach Loop Array Example

Read Now →

How to Create and Use .env File in Node JS?

Read Now →

Axios HTTP requests in Node JS Example

Read Now →

Node JS Http Request with Headers Example

Read Now →

Node JS Make HTTP Put Request Example

Read Now →

Node JS Make HTTP Delete Request Example

Read Now →

Node JS Make Http Get Request with Parameters Example

Read Now →

Node JS Multer Rename Uploaded File Example

Read Now →

Four Reasons Why Node.js is your Server-side Hero

Read Now →