Node JS Remove Empty Elements From Array

22-Aug-2022

.

Admin

Node JS Remove Empty Elements From Array

Hi Friends,

In this tutorial, I will show you node js remove empty elements from an array. you will learn to remove empty elements from the array in node.js. if you have a question about nodejs remove empty elements from an array then I will give a simple example with the solution. you'll learn how to remove empty elements from array nodejs.

There are many ways to use remove empty elements from array in node js. I will give you examples using filter(). The filter() method is used to remove empty elements from array in node js.

let's see below simple example with output:

Install Node JS


This step is not required; however, if you have not created the node js app, then you may go ahead and execute the below command:

mkdir my-app

cd my-app

npm init

Example:

index.js

//create an array list

myArray = [0, 1, null, 2, "", 3, undefined, 3, , , , , , 4, , 4, , 5, , 6, , , , ];

//remove the empty array and create a new array

newArray = myArray.filter(Number);

console.log(newArray);

Output:

[

1, 2, 3, 3,

4, 4, 5, 6

]

It will help you...

#Node JS