根据数组对象的某个字段去重

/**

  • 根据数组对象的某个字段去重
  • item.name 是[{name:1}] 根据每条数据的name值来去重
  • this.answerLiScore = this.unique(this.answerLiScore,‘name’);
  • let answerLiScore = [
    {
    name: ‘1’,
    result: ‘rewr’
    },
    {
    name: ‘1’,
    result: ‘rewr5454’
    },
    {
    name: ‘1’,
    result: ‘rewr’
    },
    {
    name: ‘4’,
    result: ‘rewr’
    }
    ];
  • */
    const uniqueArr = (arr, val) => {
    const res = new Map();
    return arr.filter(item => !res.has(item[val]) && res.set(item[val], 1))
    }

/**

  • 数组字符串字段去重
  • let answerLiScore = [1,2,2,3,4,5 ];
  • */
    answerLiScore=Array.from(new Set(answerLiScore))

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