Remove same values from array containing multiple values JavaScript


Let’s say the following is our array with similar values −

const listOfStudentName = ['John', 'Mike', 'John', 'Bob','Mike','Sam','Bob','John'];

To remove similar values from array, use the concept of set(). Following is the code −

Example

const listOfStudentName = ['John', 'Mike', 'John', 'Bob','Mike','Sam','Bob','John'];
console.log("The value="+listOfStudentName);
const doesNotContainSameElementTwice = [...new Set(listOfStudentName)];
console.log("The Array=");
console.log(doesNotContainSameElementTwice)

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo42.js.

Output

This will produce the following output −

PS C:\Users\Amit\JavaScript-code> node demo42.js
The value=John,Mike,John,Bob,Mike,Sam,Bob,John
The Array= [ 'John', 'Mike', 'Bob', 'Sam' ]

Updated on: 01-Sep-2020

105 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements