一个对象数组,根据传入的属性key对应的值来去重,实现方法如下,用双数组法,只要一次循环即可,时间复杂度低
// 对象数组去重 arr = [{a:1},{a:2},{a:1},{a:3}] function todo(pool,key) { let set = [] let result = [] for(let val of arr){ if(!set.includes(val[key])){ result.push(val) set.push(val[key]) } } return result } todo(arr,'a') // 输出[{a:1},{a:2},{a:3}]
标签:arr,set,数组,val,对象,js,result,key From: https://www.cnblogs.com/wxcbg/p/17069366.html