How to Use Array Map Filter using Node Js?

Jan 10, 2022 . Admin



Hello Friends,

This article will give you example of how to use array map filter function in node js. I explained simply about node js convert array to map filter. This tutorial will give you simple example of node js create map filter from array. This is a short guide on use node js array map filter function in nodejs

In this post, you'll learn node js array map filter example. i will show you javascript array map() filter method. we will use array map filter function given below.

Here i will give you many example of how to use array map function in node js.

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 = [
  	{ name: 'Laravel', assigned: true }, 
  	{ name: 'php', assigned: false }, 
  	{ name: 'JavaScript', assigned: true }, 
];

const filtered = temp
  	.reduce((result, { name, assigned }) => assigned ? result.push(name) && result : result, []);

console.log(filtered);

now you can simply run by following command:

node server.js
Output :
[ 'laravel', 'JavaScript' ]

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

const inputTemp = [1, 2, 3, 4, 5];
const temp = inputTemp.filter((item) => item % 2).map((item) => item + 1);

console.log(temp);

now you can simply run by following command:

node server.js
Output :
[ 2, 4, 6 ]

It will help you...

#Node JS