数组去重方法

双重循环

1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Array.prototype.unique = function () {
const newArray = [];
let isRepeat;
for (let i = 0; i < this.length; i++) {
isRepeat = false;
for (let j = 0; j < newArray.length; j++) {
if (this[i] === newArray[j]) {
isRepeat = true;
break;
}
}
if (!isRepeat) {
newArray.push(this[i]);
}
}
return newArray;
}

2.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Array.prototype.unique = function () {
const newArray = [];
let isRepeat;
for (let i = 0; i < this.length; i++) {
isRepeat = false;
for (let j = i + 1; j < this.length; j++) {
if (this[i] === this[j]) {
isRepeat = true;
break;
}
}
if (!isRepeat) {
newArray.push(this[i]);
}
}
return newArray;
}

3.

1
2
3
4
5
6
7
8
9
10
11
12
13
Array.prototype.unique = function () {
const newArray = [];

for (let i = 0; i < this.length; i++) {
for (let j = i + 1; j < this.length; j++) {
if (this[i] === this[j]) {
j = ++i;
}
}
newArray.push(this[i]);
}
return newArray;
}

Array.prototype.indexOf()

1.

1
2
3
4
5
Array.prototype.unique = function () {
return this.filter((item, index) => {
return this.indexOf(item) === index;
})
}

2.

1
2
3
4
5
6
7
8
9
10
let arr = [1, 2, 3, 22, 233, 22, 2, 233, 'a', 3, 'b', 'a'];
Array.prototype.unique = function () {
const newArray = [];
this.forEach(item => {
if (newArray.indexOf(item) === -1) {
newArray.push(item);
}
});
return newArray;
}

Array.prototype.sort()

1.

1
2
3
4
5
6
7
8
9
10
Array.prototype.unique = function () {
const newArray = [];
this.sort();
for (let i = 0; i < this.length; i++) {
if (this[i] !== this[i + 1]) {
newArray.push(this[i]);
}
}
return newArray;
}

2.

1
2
3
4
5
6
7
8
9
10
Array.prototype.unique = function () {
const newArray = [];
this.sort();
for (let i = 0; i < this.length; i++) {
if (this[i] !== newArray[newArray.length - 1]) {
newArray.push(this[i]);
}
}
return newArray;
}

###  Array.prototype.includes()

1
2
3
4
5
6
7
8
9
Array.prototype.unique = function () {
const newArray = [];
this.forEach(item => {
if (!newArray.includes(item)) {
newArray.push(item);
}
});
return newArray;
}

Array.prototype.reduce()

1
2
3
4
5
6
7
8
Array.prototype.unique = function () {
return this.sort().reduce((init, current) => {
if(init.length === 0 || init[init.length - 1] !== current){
init.push(current);
}
return init;
}, []);
}

对象键值对

1.

1
2
3
4
5
6
7
8
9
10
11
Array.prototype.unique = function () {
const newArray = [];
const tmp = {};
for (let i = 0; i < this.length; i++) {
if (!tmp[typeof this[i] + this[i]]) {
tmp[typeof this[i] + this[i]] = 1;
newArray.push(this[i]);
}
}
return newArray;
}

2.

1
2
3
4
5
6
7
8
9
10
11
12
13
Array.prototype.unique = function () {
const newArray = [];
const tmp = {};
for (let i = 0; i < this.length; i++) {
// 使用JSON.stringify()进行序列化
if (!tmp[typeof this[i] + JSON.stringify(this[i])]) {
// 将对象序列化之后作为key来使用
tmp[typeof this[i] + JSON.stringify(this[i])] = 1;
newArray.push(this[i]);
}
}
return newArray;
}

Map

1.

1
2
3
4
5
6
7
8
9
10
11
Array.prototype.unique = function () {
const newArray = [];
const tmp = new Map();
for(let i = 0; i < this.length; i++){
if(!tmp.get(this[i])){
tmp.set(this[i], 1);
newArray.push(this[i]);
}
}
return newArray;
}

2.

1
2
3
4
5
6
Array.prototype.unique = function () {
const tmp = new Map();
return this.filter(item => {
return !tmp.has(item) && tmp.set(item, 1);
})
}

Set

1
2
3
4
5
6
7
8
Array.prototype.unique = function () {
const set = new Set(this);
return Array.from(set);
}

Array.prototype.unique = function () {
return [...new Set(this)];
}

原生 js 实例封装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Array.prototype.myUnique = function () {
var obj = {};
for(var i = 0; i < this.length; i++) {
var cur = this[i];
if(obj[cur] === cur){
this[i] = this[this.length-1];
this.length--;
i--;
continue;
}
obj[cur] = cur;
}
obj = null;
return this;
}
-------------本文结束感谢您的阅读-------------

本文标题:数组去重方法

文章作者:Water

发布时间:2018年12月12日 - 15:12

最后更新:2023年08月01日 - 06:08

原始链接:https://water.buging.cn/2018/12/12/数组去重方法/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

坚持原创技术分享,您的支持将鼓励我继续创作!