欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

搞定 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);