Deduplication of a key in the table array in js

For example, the data bound to the table of Elemgnt requires a certain key to be unique

var myarry = [
    {name: 'liuyang',age :13},
    {name:'jike',age:15},
    {name:'liuyang',age:223 },
    {name:'jike',age:42},
    {name:'ligongjiu',age:22}]
// Pass in the array (table of the table) and the key of the table such as 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 right
}
var result = deweight(myarry,'name')
console.log(result)

  

result:

[ { name: 'liuyang', age: 13 },
{ name: 'jike', age: 15 },
{ name: 'ligongjiu', age: 22 } ]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324991051&siteId=291194637