deduplication 1.0.4 → 1.0.7
Sign up to get free protection for your applications and to get access to all the features.
- package/index.js +34 -4
- package/package.json +1 -1
package/index.js
CHANGED
@@ -2,17 +2,40 @@
|
|
2
2
|
* 数组去重
|
3
3
|
* @param arr 要去重的数组
|
4
4
|
* @param type number type为1表示使用set函数去重,type为2表示使用
|
5
|
+
* @param sort string sort的值为default表示不排序,为asc表示自增排序,为desc表示为自减
|
5
6
|
*/
|
6
|
-
function deduplication(arr,type=1) {
|
7
|
+
function deduplication(arr,type=1,sort='default') {
|
8
|
+
let newArr=[];
|
9
|
+
//判断去重的方式
|
7
10
|
switch(type){
|
8
11
|
case 1:
|
9
|
-
|
12
|
+
newArr=method1(arr);
|
10
13
|
break;
|
11
14
|
case 2:
|
12
|
-
|
15
|
+
newArr=method2(arr);
|
13
16
|
break;
|
14
17
|
}
|
15
18
|
|
19
|
+
let sortArr;
|
20
|
+
//判断排序的类型
|
21
|
+
switch (sort) {
|
22
|
+
case "default":
|
23
|
+
sortArr = newArr;
|
24
|
+
break;
|
25
|
+
case "asc":
|
26
|
+
sortArr = newArr.sort((a,b)=>{
|
27
|
+
return a-b;
|
28
|
+
});
|
29
|
+
break;
|
30
|
+
case "desc":
|
31
|
+
sortArr = newArr.sort((a,b)=>{
|
32
|
+
return b-a;
|
33
|
+
});
|
34
|
+
break;
|
35
|
+
}
|
36
|
+
|
37
|
+
return sortArr;
|
38
|
+
|
16
39
|
}
|
17
40
|
module.exports=deduplication;
|
18
41
|
|
@@ -23,6 +46,13 @@ function method1(arr){
|
|
23
46
|
return [...a];
|
24
47
|
}
|
25
48
|
|
49
|
+
//indexOf方法
|
26
50
|
function method2(arr) {
|
27
|
-
|
51
|
+
let arr2 = [];
|
52
|
+
for(let i=0;i<arr.length;i++){
|
53
|
+
if(arr2.indexOf(arr[i]) === -1){
|
54
|
+
arr2.push(arr[i]);
|
55
|
+
}
|
56
|
+
}
|
57
|
+
return arr2;
|
28
58
|
}
|