How To Use Array Remove Duplicates Using Node Js?


Share:
Published By: Admin, Published On: Jan 05, 2022, Category: Node JS


Hello Friends,

This tutorial will give you example of how to use array remove duplicates example in nodejs. i would like to share with you array remove duplicates using node js code. it's simple example of how to use array remove duplicates. i explained simply step by step how to array remove duplicates using nodejs.

Here, i will give you very simple step to remove duplicates from the array in nodejs. Today, i will give you simple example of array remove duplicate in nodejs.

so let's follow bellow steps:

Here i will give you many example how to use array remove duplicates using 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 items = [1, 2, 5, 1, 3, 4, 5]

const uniqueItems = Array.from(new Set(items)) 

console.log(uniqueItems) 

now you can simply run by following command:

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

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

let subjects = ["laravel", "php", "bootstrap", "php", "laravel","nodejs"]  

let useFilter = () => {  
    return unique = subjects.filter(function(item,index){  
        return subjects.indexOf(item) == index;  
    });  
} 
 
console.log(useFilter()) 

now you can simply run by following command:

node server.js
Output :
[ 'laravel', 'php', 'bootstrap', 'nodejs' ]

It will help you...

Share: