思路:遍历放对象里,id是键,重复的后面就把前面的覆盖了,然后再转数组
const data = [
{ id: 1, name: '张三', age: 15, },
{ id: 2, name: 'John', age: 18, },
{ id: 3, name: '李四', age: 18, },
{ id: 1, name: '张三', age: 15, },
{ id: 4, name: 'Jack', age: 18, },
{ id: 5, name: '王五', age: 10, },
{ id: 4, name: 'Jack', age: 18, },
{ id: 2, name: 'John', age: 18, },
];
let newObj={}
data.forEach(item => {
newObj[item.id]=item
});
console.log(newObj)
var arr = []
for (let i in newObj) {
arr.push(newObj[i])
}
console.log(arr);