JS array: Remove duplicate values from

mobin12

Yoctopat
Katılım
14 Ağustos 2023
Mesajlar
1
Daha fazla  
Cinsiyet
Kadın
I have a Very Basic array of JavaScript that might or might not have duplicates.

var names = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"]; i need to get rid of duplicate values and add the unique One S to a New array.

I could list every piece of code I've ever used, but since they all fail, I don't believe it's worth it. I also accept jquery-based solutions.
 
Son düzenleme:
Here are a few ways to remove duplicate values from an array in JavaScript:
I have used this example from here: How to remove Duplicates from Array
Using a Set


JavaScript:
var names = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"];

// Create a set to store the unique elements of the array.
const uniqueNames = new Set(names);

// Get the array of elements in the set.
const newNames = Array.from(uniqueNames);

console.log(newNames); // ["Adam", "Jenny", "Carl", "Mike", "Matt"]
This code works by first creating a set to store the unique elements of the array. Then, it gets the array of elements in the set and returns it.
 

Technopat Haberler

Geri
Yukarı