数组对象根据某个key的值去重

转载于:https://www.cnblogs.com/jack-liu6/p/8967680.html

var myarry = [

    {name: 'liuyang',age :13},

    {name:'jike',age:15},

    {name:'liuyang',age:223 },

    {name:'jike',age:42},

    {name:'ligongjiu',age:22} ]

// 传入数组(表格的table)和表格的key如name

function deweight (arr, key) {

    let ret = []

    arr.forEach((item, index, self) => {

        let compare = []

        ret.forEach((retitem, retindex, retself) => {

        compare.push(retitem[key])

        })

        if (compare.indexOf(item[key]) === -1) {

        ret.push(item)

        }

    })

    return ret

}

var result = deweight(myarry,'name')

console.log(result)


结果:

[ { name: 'liuyang', age: 13 },

{ name: 'jike', age: 15 },

{ name: 'ligongjiu', age: 22 } ]


原文链接:http://www.cnblogs.com/jack-liu6/p/8967680.html

你可能感兴趣的:(数组对象根据某个key的值去重)