js数组去重
2021-04-12 15:31
标签:include OLE ++ ret 实现 href 循环 数组去重 rom 转载自 JS实现数组去重方法整理 js数组去重 标签:include OLE ++ ret 实现 href 循环 数组去重 rom 原文地址:https://www.cnblogs.com/justyouadmin/p/13349498.html// 数组去重方法
let arr1 = Array.from(new Array(10000), (item, index) => {
return index;
})
let arr2 = Array.from(new Array(5000), (item, index) => {
return index + index
})
console.log([...arr1, ...arr2])
// 方法一:Array.filter() + indexOf
function distinct(a, b) {
let arr = a.concat(b);
return arr.filter((item, index) => {
return arr.indexOf(item) === index
})
}
// 方法二: for...of + includes()
function distinct(a, b) {
let arr = a.concat(b);
let result = [];
for (let i of arr) {
!result.includes(i) && result.push(i)
}
return result;
}
// 方法三:双重for循环
function distinct(a, b) {
let arr = a.concat(b);
for (let i = 0, len = arr.length; i