对象数组去重
用ES6的reduce方法
this.backTag = [
{id:'111',name:'哈哈哈1'},
{id:'222',name:'哈哈哈2'},
{id:'333',name:'哈哈哈3'},
{id:'444',name:'哈哈哈4'},
{id:'111',name:'哈哈哈5'}];
let obj = {}
this.backTag = this.backTag.reduce(function(item, next) {
obj[next.id] ? '' : obj[next.id] = true && item.push(next);
return item;
}, []);
console.log(this.backTag)
reduce 语法
array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
| 参数 | 描述 |
|---|---|
| function(total,currentValue, index,arr) | 必需。用于执行每个数组元素的函数。 |
| initialValue | 可选。传递给函数的初始值 |
function(total,currentValue, index,arr)
| 参数 | 描述 |
|---|---|
| total | 必需。初始值, 或者计算结束后的返回值。 |
| currentValue | 必需。当前元素 |
| currentIndex | 可选。当前元素的索引 |
| arr | 可选。当前元素所属的数组对象。 |
本文介绍如何使用 ES6 的 reduce 方法实现对象数组的去重操作,并提供了详细的代码示例。通过此方法可以有效地移除数组中重复的对象。

4万+

被折叠的 条评论
为什么被折叠?



