美文网首页
ES6数组去重

ES6数组去重

作者: 飘扬灬红领巾 | 来源:发表于2021-01-20 09:48 被阅读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],[]);

相关文章

网友评论

      本文标题:ES6数组去重

      本文链接:https://www.haomeiwen.com/subject/xobhzktx.html