getChongfu(list, field) {
//数据
const key = {} //存储的 key 是type的值,value是在indeces中对应数组的下标
const indices = [] //数组中每一个值是一个数组,数组中的每一个元素是原数组中相同type的下标
list.map((item, index) => {
//根据对应字段 分类(type)
const type = item[field]
const _index = key[type]
if (_index !== undefined) {
indices[_index].push(index)
} else {
key[type] = indices.length
indices.push([index])
}
})
// 归类结果
const result = []
indices.map((item) => {
item.map((index) => {
//result.push(List[index]) 相同项排序在一起
//if (item.length > 1) {} 只要重复项
//if (item.length == 1){} 只要单独项
//我这里需要重复项 根据业务处理
if (item.length > 1) {
result.push(list[index])
}
})
})
//结果
return result
}
js数组对象中查找重复的元素,并返回数组
最新推荐文章于 2024-04-26 16:46:19 发布