只需遍历一遍数组
1 对象数组
fun(data: Array<any>) { const obj: any = {}; const result = []; for (const item of data) { if (!obj[item.Id]) { result.push(item); obj[item.Id] = 1; } } return result; }
2 普通数组
fun(data) { const obj: any = {}; const result = []; for (const item of data) { if (!obj[item]) { result.push(item); obj[item] = 1; } } return result; }
3 两个数组
fun(a: any[], b: any[]): any[] { const arr = a.concat(b); const result = []; const obj: any = {}; for (const i of arr) { if (!obj[i]) { result.push(i); obj[i] = 1; } } return result; }
原文:https://www.cnblogs.com/tingying/p/14346280.html