-
Set data structure is added to the >ES6, similar to an array, but its members are unique &NBSP, whose constructor can accept an array as a parameter, such as
let array = [1, 1, 1, 1, 2, 3, 4, 4, 5, 3]; let set = new Set (array); console.log (set); //= = Set {1, 2, 3, 4, 5}
Arraya 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]
Ext.: http://www.cnblogs.com/raocheng/articles/6549556.html
/span>
Set to implement array de-weight