先介绍ES6 set去重的方法(两种)
var myArr = [1,2,3,4,5,1,2,3]; Array.from(new Set(myArr));
var newArr = [ ...myArr ]
这些都是一般数组去重的方式,如果遇到数组对象的形式 [ {name:'123'} , {name:'123'} , {name:'456'} , {name:'456'} ] , 需要用对象中的某个键值对进行匹配去重
var arr = [ { "CompanySerial": "123", "OldDeviceId": "id111", "HandleState": 2 }, { "CompanySerial": "456", "OldDeviceId": "id111", "HandleState": 2 }, { "CompanySerial": "789", "OldDeviceId": "id222", "HandleState": 1 }] function fn(arr) { const res = new Map(); return arr.filter(a=>{ return !res.has(a.OldDeviceId) && res.set(a.OldDeviceId,1); }) } var myData = fn(arr); console.log(myData); //结果你们自己看 ^ ~ ^