方法一
let list = [{
"key": 108,
"label": "陈xx,
"state": 1
},
{
"key": 10354,
"label": "黄科军",
"state": 2
},
{
"key": 10379,
"label": "李嘉泰",
"state": 2
},
{
"key": 13751,
"label": "洪鍠淮",
"state": 1
},
{
"key": 10354,
"label": "黄科军",
"state": 1
},
{
"key": 10379,
"label": "李嘉泰",
"state": 1
}
]
let c = []
list.forEach((element, index) => {
if (contrast(element, c)) {
c.push(element)
}
});
console.log(c);
function contrast(map, listA) {
// console.log(listA);
let a = true;
listA.forEach((element, index) => {
// console.log(list);
// console.log(element.key, map.key);
if (element.key === map.key) {
a = false
}
});
return a
}
方法二
//key是你要对比的值
function contrast(arr) {
const map = new Map()
const result = []
for (let i = 0; i < arr.length; i++) {
if (map.has(arr[i].key)) {
map.set(arr[i].key, true)
// console.log(map);
} else {
map.set(arr[i].key, false)
// console.log(map);
result.push(arr[i])
}
}
return result
}