搞定 JS 数组重复问题:从简单到复杂全方位解析
最编程
2024-02-02 07:49:52
...
function soloArray(array, UID) {
let newArr = [];
let UIDlist = [];
array.forEach((item) => {
if (!UIDlist.includes(item[UID])) {
UIDlist.push(item[UID]);
newArr.push(item);
}
});
return newArr;
}
let oldList = [
{
ID: 1,
name: "王小虎",
age: 10,
},
{
ID: 2,
name: "张三",
age: 20,
},
{
ID: 2,
name: "张三",
age: 20,
},
{
ID: 3,
name: "李四",
age: 30,
},
];
let newList = soloArray(oldList, "ID");
console.log(newList);
let newArr = [];
let UIDlist = [];
array.forEach((item) => {
if (!UIDlist.includes(item[UID])) {
UIDlist.push(item[UID]);
newArr.push(item);
}
});
return newArr;
}
let oldList = [
{
ID: 1,
name: "王小虎",
age: 10,
},
{
ID: 2,
name: "张三",
age: 20,
},
{
ID: 2,
name: "张三",
age: 20,
},
{
ID: 3,
name: "李四",
age: 30,
},
];
let newList = soloArray(oldList, "ID");
console.log(newList);
上一篇: 去除Java中的重复字段和对象