一、根据数组中唯一的值去重, cur原数组, next要添加的数据
this.warnSiteWords.push({one:this.valueUrl,two:this.valueName});
let obj = {};
// 根据数组某一个值去重 cur原数组, next要添加的数据
let reduce = this.warnSiteWords.reduce((cur, next) => {
obj[next.id] ? "" : (obj[next.id] = true && cur.push(next));
return cur;
}, []);
this.warnSiteWords = reduce;
二、数组对象去重,匹配对象的每一个值
// 数组对象去重,匹配对象的每一个值
if (this.warnSiteWords.find(p => p.one == this.valueUrl && p.two == this.valueName) == undefined) {
this.warnSiteWords.push({one:this.valueUrl,two:this.valueName});
}
三、封装方法
新建js,内容如下,在组件中引用该js
1、新建js
// 数组对象去重(满足多个值同时匹配)
export function deWeightFour(arr) {
let arrTemp = [];
arr = arr.reduce(function (a, b) {
if ( arrTemp.indexOf(b.firstWord+b.secondWord)==-1 ) {
a.push(b)
arrTemp.push(b.firstWord+b.secondWord)
}
return a
}, [])
return arr
}
export default {
deWeightFour,
}
2、引用
import validate from '@/utils/validate.js'
3、使用
let newArr = validate.deWeightFour(arr)
this.warnAuthorWords = newArr;
版权声明:本文为weixin_43285861原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。