数组对象去重算法
前言:
将一个包含重复数据的数组对象进行去重操作
function getOnlyData(data, field) {
let result = []
const suggestionMap = {}
const length = data.length
if (length === 0) {
return result
}
for (let i = 0; i < length; i++) {
let currentItem = data[i]
let suggestion = currentItem[field]
if (!suggestionMap[suggestion]) {
suggestionMap[suggestion] = true
result.push(currentItem)
}
}
return result
}
let data = [{name:'张三',value:'1'},{name:'张三',value:'2'},{name:'李四',value:'3'}]
getOnlyData(data,'name')
/* [
{
"name": "张三",
"value": "1"
},
{
"name": "李四",
"value": "3"
}
]*/
分类:
js
创建时间:
2023-11-29 16:17:10
更新时间:
2023-11-29 16:23:46