js根据多个属性去重
版本1: function process(arr, ...rest) { const cache = []; let t; function check(item) { let trueList = []; let args; for(args of rest) { trueList.push(item[args] === t[args]) } return trueList.every(item => item) } for (t of arr) { if (cache.find(check)) { continue; } cache.push(t); } return cache; } 版本2: function unique(list, ...rest) { if (Object.prototype.toString.call(list).indexOf('Array') < 0 || list.length === 0) return [] const result = {} for (const item of list) { const key = rest.map(key => item[key]).join('|') result[key] = item } return Object.values(result) }
转载随笔/文章请保留出处和署名,谢谢!!

浙公网安备 33010602011771号