数组去重
方案一、双层 for 循环和 splice()
描述: 通过双层 for 循环,外层循环元素,内层比较值,值相同时,利用 splice 删除这个值,将数组长度变量减一
实现:
function unique(array) {
let length = array.length;
for (let i = 0; i < length; i++) {
for (let j = i + 1; j < length; j++) {
if (array[i] === array[j]) {
array.splice(j, 1);
length--;
}
}
}
}
const array = [
34,
23,
53,
23,
53,
"张文强",
"张文强",
null,
null,
undefined,
undefined,
{},
{},
23,
"张文强",
34,
];
unique(array);
console.log(array);
方案二、双层 for 循环和 push()
描述: 通过双层 for 循环,外层循环元素,内层比较值,值相同时,内外层同时跳过,内层循环完毕后,push 进新数组。
实现:
function unique(array) {
const result = [];
let length = array.length;
for (let i = 0; i < length; i++) {
for (let j = i + 1; j < length; j++) {
if (array[i] === array[j]) {
j = ++i;
}
}
result.push(array[i]);
}
return result;
}
const array = [
34,
23,
53,
23,
53,
"张文强",
"张文强",
null,
null,
undefined,
undefined,
{},
{},
23,
"张文强",
34,
];
const result = unique(array);
console.log(result);
方案三、一层 for 和 indexOf() 、push()
描述: 通过一层 for 循环遍历,新数组通过indexOf检查是否存在当前元素,不存在,则新增元素
实现:
function unique(array) {
const result = [];
let length = array.length;
for (let i = 0; i < length; i++) {
if (result.indexOf(array[i]) === -1) {
result.push(array[i]);
}
}
return result;
}
const array = [
34,
23,
53,
23,
53,
"张文强",
"张文强",
null,
null,
undefined,
undefined,
{},
{},
23,
"张文强",
34,
];
const result = unique(array);
console.log(result);
方案四、一层 for 和 includes() 、push()
描述: 通过一层 for 循环遍历,新数组通过includes检查是否存在当前元素,不存在,则新增元素
实现:
function unique(array) {
const result = [];
let length = array.length;
for (let i = 0; i < length; i++) {
if (!result.includes(array[i])) {
result.push(array[i]);
}
}
return result;
}
const array = [
34,
23,
53,
23,
53,
"张文强",
"张文强",
null,
null,
undefined,
undefined,
{},
{},
23,
"张文强",
34,
];
const result = unique(array);
console.log(result);
方案五、一层 for 和 find() 、push()
描述: 通过一层 for 循环遍历,新数组通过find检查是否存在当前元素,不存在,则新增元素
实现:
function unique(array) {
const result = [];
let length = array.length;
for (let i = 0; i < length; i++) {
if (!result.find((value, index) => value == array[i])) {
result.push(array[i]);
}
}
return result;
}
const array = [
34,
23,
53,
23,
53,
"张文强",
"张文强",
null,
null,
undefined,
undefined,
{},
{},
23,
"张文强",
34,
];
const result = unique(array);
console.log(result);
方案六、一层 for 和 findIndex() 、push()
描述: 通过一层 for 循环遍历,新数组通过findIndex检查是否存在当前元素,不存在,则新增元素
实现:
function unique(array) {
const result = [];
let length = array.length;
for (let i = 0; i < length; i++) {
if (result.findIndex((value, index) => value == array[i]) === -1) {
result.push(array[i]);
}
}
return result;
}
const array = [
34,
23,
53,
23,
53,
"张文强",
"张文强",
null,
null,
undefined,
undefined,
{},
{},
23,
"张文强",
34,
];
const result = unique(array);
console.log(result);
方案七、filter() 和 indexOf() 搭配去重
描述: indexOf() 方法可以得到数组元素第一次匹配时的索引,那么再通过 filter() 进行筛选过滤即可
实现:
function unique(array) {
return array.filter((filterValue, filterIndex) => {
return array.indexOf(filterValue) === filterIndex;
});
}
const array = [
34,
23,
53,
23,
53,
"张文强",
"张文强",
null,
null,
undefined,
undefined,
{},
{},
23,
"张文强",
34,
];
const result = unique(array);
console.log(result);
方案八、filter() 和 findIndex() 搭配去重
描述: findIndex() 方法可以得到数组元素第一次匹配时的索引,那么再通过 filter() 进行筛选过滤即可
实现:
function unique(array) {
return array.filter((filterValue, filterIndex) => {
return (
array.findIndex(
(findIndexValue, findIndexIndex) => findIndexValue == filterValue
) === filterIndex
);
});
}
const array = [
34,
23,
53,
23,
53,
"张文强",
"张文强",
null,
null,
undefined,
undefined,
{},
{},
23,
"张文强",
34,
];
const result = unique(array);
console.log(result);
方案九、reduce() 和 includes() 搭配去重
描述: 通过 reduce 根据 includes 的结果累加数组元素
实现:
function unique(array) {
return array.reduce((accumulator, currentValue) => {
return accumulator.concat(
accumulator.includes(currentValue) ? [] : currentValue
);
}, []);
}
const array = [
34,
23,
53,
23,
53,
"张文强",
"张文强",
null,
null,
undefined,
undefined,
{},
{},
23,
"张文强",
34,
];
const result = unique(array);
console.log(result);
方案十、reduce() 和 indexOf() 搭配去重
描述: 通过 reduce 根据 indexOf 的结果累加数组元素
实现:
function unique(array) {
return array.reduce((accumulator, currentValue) => {
return accumulator.concat(
accumulator.indexOf(currentValue) === -1 ? currentValue : []
);
}, []);
}
const array = [
34,
23,
53,
23,
53,
"张文强",
"张文强",
null,
null,
undefined,
undefined,
{},
{},
23,
"张文强",
34,
];
const result = unique(array);
console.log(result);
方案十一、reduce() 和 find() 搭配去重
描述: 通过 reduce 根据 find() 的结果累加数组元素
实现:
function unique(array) {
return array.reduce((accumulator, currentValue) => {
return accumulator.concat(
accumulator.find((findValue) => findValue == currentValue)
? []
: currentValue
);
}, []);
}
const array = [
34,
23,
53,
23,
53,
"张文强",
"张文强",
null,
null,
undefined,
undefined,
{},
{},
23,
"张文强",
34,
];
const result = unique(array);
console.log(result);
方案十二、reduce() 和 findIndex() 搭配去重
描述: 通过 reduce 根据 findIndex() 的结果累加数组元素
实现:
function unique(array) {
return array.reduce((accumulator, currentValue) => {
return accumulator.concat(
accumulator.findIndex(
(findIndexValue) => findIndexValue == currentValue
) === -1
? currentValue
: []
);
}, []);
}
const array = [
34,
23,
53,
23,
53,
"张文强",
"张文强",
null,
null,
undefined,
undefined,
{},
{},
23,
"张文强",
34,
];
const result = unique(array);
console.log(result);
方案十三、通过对象属性不能重复
描述: 设置新的空数组和空对象,如果新对象中通过Object.prototype.hasOwnProperty.call 没有数组元素属性,那么新增元素
实现:
function unique(array) {
const result = [];
const resultMap = {};
const length = array.length;
for (let i = 0; i < length; i++) {
if (!Object.prototype.hasOwnProperty.call(resultMap, array[i])) {
resultMap[array[i]] = true;
result.push(array[i]);
}
}
return result;
}
const array = [
34,
23,
53,
23,
53,
"张文强",
"张文强",
null,
null,
undefined,
undefined,
{},
{},
23,
"张文强",
34,
];
const result = unique(array);
console.log(result);
方案十四、通过 Set 元素不会重复
描述: set 中的元素唯一
实现:
function unique(array) {
return Array.from(new Set([...array]));
}
const array = [
34,
23,
53,
23,
53,
"张文强",
"张文强",
null,
null,
undefined,
undefined,
{},
{},
23,
"张文强",
34,
];
const result = unique(array);
console.log(result);
方案十五、通过 Map 的 key 不会重复
描述: map 中的 key 唯一
实现:
function unique(array) {
const result = [];
const map = new Map();
const length = array.length;
for (let i = 0; i < length; i++) {
if (!map.has(array[i])) {
map.set(array[i], true);
result.push(array[i]);
}
}
return result;
}
const array = [
34,
23,
53,
23,
53,
"张文强",
"张文强",
null,
null,
undefined,
undefined,
{},
{},
23,
"张文强",
34,
];
const result = unique(array);
console.log(result);