es6 最新数据去重 Array.from
数组去重:set有去重功能,返回类数组,通过 延展符/Array.from 转成数组
let arr = [1,1,1,1,2,2,2,3,3,3,5,6,7,1,1,1,1,2,2,2,3,3,3,5,6,7];
[…new Set(arr)]; // [1, 2, 3, 5, 6, 7]
Array.from(new Set(arr)); // [1, 2, 3, 5, 6, 7]
new Set(),用来去重数组。
Set类似于数组,区别在于它所有的成员都是唯一的,不能有重复的值
数值去重:
let arr = [1, 2, 2, 3]; // [1, 2, 2, 3]
let set = new Set(arr); //去掉重复数据,返回结果是’set’
let newArr = Array.from(set); //将set转化为数组
console.log(newArr); // [1, 2, 3]
对象去重:
const b={a:2}
let arr = [{a:1}, b, b, {a:3}]; //[{a:1},{a:2},{a:2},{a:3}]
let set = new Set(arr);
let newArr = Array.from(set);
console.log(newArr); // [{a:1},{a:2},{a:3}]
无法对象去重:
let arr = [{a:1}, {a:2}, {a:2}, {a:3}]; //[{a:1},{a:2},{a:2},{a:3}]
let set = new Set(arr);
let newArr = Array.from(set);
console.log(newArr); //[{a:1},{a:2},{a:2},{a:3}]