Divided into two cases: 1 ordinary array; 2 object array
1 ordinary array
const arr=[1,2,3,2,5,6,3]
const newarr=[...new Set(arr)]
console.log(newarr)//[1,2,3,5,6]
2 object array, if there is the following array
const data = [
{ name: '张三', mobile: '13811112222', idCard: '1234567890' },
{ name: '李四', mobile: '13911113333', idCard: '2345678901' },
{ name: '王五', mobile: '13811112222', idCard: '3456789012' },
{ name: '赵六', mobile: '13711114444', idCard: '4567890123' },
{ name: '钱七', mobile: '13911113333', idCard: '5678901234' }
];
The purpose of the operation is to filter out records with unique mobile fields
First take out all the mobile field values in the data
const mobile=data.map(item=>item.mobile)
The second step is to deduplicate mobile
const newmobile=[...new Set(mobile)]
The third step is to traverse the data with the value in newmobile and filter out the corresponding records
const newdata=mobilenew.map(item=>{
return data.find(item=>item.mobile===item)
})
Complete code implementation
const data = [
{ name: '张三', mobile: '13811112222', idCard: '1234567890' },
{ name: '李四', mobile: '13911113333', idCard: '2345678901' },
{ name: '王五', mobile: '13811112222', idCard: '3456789012' },
{ name: '赵六', mobile: '13711114444', idCard: '4567890123' },
{ name: '钱七', mobile: '13911113333', idCard: '5678901234' }
];
const mobile=data.map(item=>item.mobile)
const mobilenew=[...new Set(mobile)]
const newdat=mobilenew.map(item=>{
return data.find(itmes=>itmes.mobile===item)
})
console.log(newdat)
// [
// { name: '张三', mobile: '13811112222', idCard: '1234567890' },
// { name: '李四', mobile: '13911113333', idCard: '2345678901' },
// { name: '赵六', mobile: '13711114444', idCard: '4567890123' }
// ]