When you remove the duplicate numbers from an array with new Set()
method, it turns into a set instead of an array like this
let duplicates = [1,2,3,4,5,6,2];
// remove the duplicates
let noDuplicates = new Set(duplicates);
// output Set {1,2,3,4,5,6}
Duplicates may happen by posting/collecting same value from one/different sources, or by concat()
arrays.
And you can convert that set to an array again.
Solution 1:
let duplicates = [1,2,3,4,5,6,2];
// remove the duplicates
let noDuplicates = new Set(duplicates);
// output {1,2,3,4,5,6}
let arrayWithNoDuplicates = Array.from(noDuplicates);
// output [1,2,3,4,5,6]
Solution 2:
let duplicates = [1,2,3,4,5,6,2];
// remove the duplicates
let noDuplicates = new Set(duplicates);
// output {1,2,3,4,5,6}
let arrayWithNoDuplicates = [...noDupicates];
// output [1,2,3,4,5,6]
Solution 3:
let duplicates = [1,2,3,4,5,6,2];
let noDuplicates = Array.from(new Set(duplicates))
// output [1,2,3,4,5,6]
Solution 4:
let duplicates = [1,2,3,4,5,6,2];
let noDuplicates = [... new Set(duplicates)];
// output [1,2,3,4,5,6]
Apply
let a = [1,2,3,4];
let b = [5,6,2];
let c = a.concat(b);
let d = new Set(c);
let e = Array.from(d);
// or in one line
let f = Array.from(new Set(a.concat(b)));
Source : How to convert a Set to an Array? – JavaScript | tradecoder
Top comments (0)