ES6 数组去重

/**
 * 数组去重
 */
global.unique = function(arr, ... keys){
    if(keys.length == 0){
        keys = ['id'];
    }
    let exists = new Set();
    
    let result = arr.reduce(function(dist, item){
        
        let karr = [];
        keys.forEach(k =>{
            karr.push(item[k]);
        });
        let kstr = karr.join("#\n#");
        if(!exists.has(kstr)){
            dist.push(item);
            exists.add(kstr);
        }
        return dist;
    }, [])
    
    return result;
}

 

posted @ 2021-03-25 13:43  rubekid  阅读(18)  评论(0编辑  收藏  举报