const  aa = [
    {name:'aaa',age:1},
    { name: 'bbb', age: 2 },
    { name: 'cccc', age: 3 },
    { name: 'aaa', age: 1 },
    { name: 'bbb', age: 5 },
    { name: 'aaa', age: 6 }
]
nosameArr(arrNum,'age')
/**
 * 去重 
 * arg[0]  Array  需要去重的数组
 * arg[1]    需要去重的key
 * return [Array]
 */
function nosameArr(){
    let args = Array.prototype.slice.call(arguments)
    let key = args[1]
    return args[0].reduce((pre, cur, index, arr) => {
        if (typeof key == 'undefined') {
            if (JSON.stringify(pre).indexOf(JSON.stringify(cur)) < 0) {
                // console.log(pre, cur)
                return pre.concat(cur)
            } else {
                return pre
            }
        }
        if (pre.length == 0){
            return pre.concat(cur)
        }
        for (let i in pre) {
            if (pre[i][key] != cur[key]){
                return pre.concat(cur)
            }else{
                return pre
            }
        }
    }, [])
}

 

posted on 2020-04-13 15:32  chenlw101  阅读(190)  评论(0编辑  收藏  举报