数组对象去重复的原理很简单,就是利用对象,对象的key值只有一个。代码如下
const template = [
{
group_id: 1,
name: 1
},
{
group_id: 2,
name: 2
},
{
group_id: 3,
name: 3
},
{
group_id: 1,
name: 1
},
{
group_id: 2,
name: 2
},
]
const hash = {}
let templateList = template.reduceRight((item, next) => {
if (!hash[next.group_id]) hash[next.group_id] = true && item.push(next)
return item
}, [])
console.log(templateList)
欢迎关注我的个人技术博客 javascript艺术