How to Use Array Remove Element using Node Js?

Jan 04, 2022 . Admin



Hello Friends,

This article will give you example of array remove element in nodejs. I explained simply about remove specific element from array in nodejs. This tutorial will give you simple example of Splice to Remove Array Elements. This is a short guide on use Removing Array Items By Value Using Splice in nodejs

In this post, You'll learn Removing Array Items By Value Using Splice. i will show you Array filter Method to Remove Items By Value nodejs.We will use splice and filter method to removing array item.

Here i will give you many example how to use array remove element using node js.

So, let's see bellow solution:

Example 1 :

Step 1 : Create Node App

Run following command to create node app.

mkdir my-app
cd my-app
 
npm init
Step 2 : Create server.js file

server.js

const temp = [2, 5, 9];

console.log(temp);

const index = temp.indexOf(5);
if (index > -1) {
    temp.splice(index, 1);
}

console.log(temp); 

now you can simply run by following command:

node server.js
Output :
[ 2, 5, 9 ]
[ 2, 9 ]

Example 2 :

Step 1 : Create Node App

Run following command to create node app.

mkdir my-app
cd my-app
 
npm init
Step 2 : Create server.js file

server.js

var value = 4

var temp = [1, 2, 3, 4, 5]

temp = temp.filter(function(item) {
    return item !== value
})

console.log(temp)

now you can simply run by following command:

node server.js
Output :
[ 1, 2, 3, 5 ]

It will help you...

#Node JS