Tag:for node each Code strong A line of code ber color class
- The
Set
data structure is new in
-
ES6, similar to an array, but its members are unique its constructors can accept an array as an argument, such as:
let array = [1, 1, 1, 1, 2, 3, 4, 4, 5, Span class= "Hljs-number" >3]; let set = new Set (array); console.log (set); //= = Set {1, 2, 3, 4, 5}
Array
a static method has been added to the ES6 to Array.from
convert an array-like object into arrays, such as through querySelectAll
methods HTML DOM Node List
, as well as new Set
and Map
other traversed objects in the ES6, such as:
let set = new Set(); set.add(1).add(2).add(3); let array = Array.from(set); console.log(array); // => [1, 2, 3]
So now we can use a line of code to achieve the weight of the array:
let array = Array.from(new Set([1, 1, 1, 2, 3, 2, 4]));console.log(array);// => [1, 2, 3, 4]
Attached: ES5 implementing array de-weight
var array = [1, ‘1‘, 1, 2, 3, 2, 4];var tmpObj = {};var result = [];array.forEach(function(a) { var key = (typeof a) + a; if (!tmpObj[key]) { tmpObj[key] = true; result.push(a); }});console.log(result);// => [1, "1", 2, 3, 4]
One line of code implements array de-weight (ES6)