Remove same values from array containing multiple values JavaScript

JavascriptWeb DevelopmentObject Oriented Programming

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' ]
raja
Published on 01-Sep-2020 15:43:37
Advertisements