-
Notifications
You must be signed in to change notification settings - Fork 0
/
SplArray.js
158 lines (155 loc) · 5.07 KB
/
SplArray.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
!function (root, factory) {
if (typeof exports == 'object' && typeof module != 'undefined') {
module.exports = factory()
} else if (typeof define == 'function' && define.amd) {
define(factory)
} else {
var moduleName = 'SplArray';
root[moduleName] = factory()
}
}(this, function () {
var SplArray = {
//删除一维数组指定值
remove: function (array, value) {
return array.filter(function (value2) {
return value != value2;
})
},
//一维数组排序(type:1从小到大,2从大到小,3随机)
sort:function(array,type){
type = type || 1;
return array.sort(function (a,b) {
switch (type) {
case 1:
return a - b;
case 2:
return b - a;
case 3:
return Math.random() - 0.5;
default:
return array;
}
})
},
//二维数组排序,支持多个字段,中文排序
sort_2: function (array, array_sort) {
function sort2_sort(a, b, array_sort, n) {
var array = array_sort[n].split(' ');
var key = array[0];
var sort = array[1];
if (a[key] === b[key]) {
n++;
if (n < array_sort.length) {
return sort2_sort(a, b, array_sort, n);
}
}
if (sort == 'asc') {
return a[key].localeCompare(b[key]);
} else {
return b[key].localeCompare(a[key]);
}
}
for (var i = 0; i < array_sort.length; i++) {
array.sort(function (a, b) {
return sort2_sort(a, b, array_sort, 0)
});
}
return array;
},
//获取二维数组指定列的值
array_column: function (array, key) {
var column = [];
for (var i in array) {
if (array[i].hasOwnProperty(key)) {
column.push(array[i][key]);
}
}
return column;
},
//获取数组交集
array_intersect: function (array1, array2) {
return array1.filter(function (value) {
return array2.indexOf(value) !== -1;
})
},
//获取数组差集
array_diff: function (array1, array2) {
return array1.concat(array2).filter(function (value) {
return array2.indexOf(value) === -1;
})
},
//获取数组差集(所有)
array_diff_all: function (array1, array2) {
return array1.concat(array2).filter(function (value) {
return array1.indexOf(value) === -1 || array2.indexOf(value) === -1;
});
},
//获取数组并集
array_union: function (array1, array2) {
return array1.concat(array2.filter(function (value) {
return array1.indexOf(value) === -1;
}));
},
//数组去重(高性能)。跟并集功能类似
array_unique: function (array) {
//利用对象属性不会重复特性
var result = [];
var obj = {};
for (var i in array) {
if (!obj[array[i]]) {
result.push(array[i]);
obj[array[i]] = 1;
}
}
return result;
},
//数组转字符串
implode: function (glue, array) {
glue = glue || '';
return array.join(glue);
},
explode: function (delimiter, string) {
delimiter = delimiter || '';
return string.split(delimiter);
},
//去空值
filter_empty: function (array) {
return array.filter(function (val) {
return !(!val || val === "");
});
},
//判断是否在数组中
in_array:function (array,value) {
return array.indexOf(value) != -1 ? true : false;
},
//将类数组转为数组
formArray:function (array) {
var arr= [];
if (Array.isArray(array)) {
arr = array;
} else {
arr = Array.prototype.slice.call(array);
}
return arr;
},
//获取最大值
max:function (array) {
return Math.max.apply(null, array);
},
//获取最小值
min:function (array) {
return Math.min.apply(null, array);
},
//求和
sum:function (array) {
return array.reduce((pre, cur) => {
return pre + cur
})
},
//平均值
average:function (array) {
return this.sum(array) / array.length
}
};
return SplArray;
});