ES6数组去重

2021-01-20  本文已影响0人  飘扬灬红领巾

普通数组去重

方法1
let schoolIdsList = ["x001","x002","x003","x001","x002","x001"];
let newSchoolIdsList = [...new Set(schoolIdsList )];
console.log(newSchoolIdsList );    // [ 'x001', 'x002', 'x003']
方法2
let arr = [1, 2, 3, 2, 1];

function unique(arr){
    return Array.from(new Set(arr));
}
console.log(unique(arr))   // [1, 2, 3]

对象数组去重

let  qu = res.list.map(item=>({
                    areaId:item.areaId,
                    areaName:item.areaName,
                    cho:false
                }))
let areaId = 'areaId';
let qu1 = qu.reduce((all, next) => all.some((atom) => atom[areaId] == next[areaId]) ? all : [...all, next],[]);
上一篇 下一篇

猜你喜欢

热点阅读