-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessArray&ArrayObject.js
More file actions
136 lines (127 loc) · 3.75 KB
/
processArray&ArrayObject.js
File metadata and controls
136 lines (127 loc) · 3.75 KB
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
/**
* 数组对象去重
* arr:待去重的数组对象
* fieldId:以此属性值作为去重判断依据
* [{fieldId:1,name:a,...},{fieldId:2, name:b,...},...]
*/
export function uniqueArrObj (arr, fieldId) {
if (!Array.isArray(arr)) {
console.error('arr参数必须是一个数组,将原样返回输入参数')
return arr
}
// 不提供去重id则直接返回原数组
if (typeof fieldId !== 'string' || !fieldId) {
console.error('fieldId没有提供,将原样返回输入参数')
return arr
}
const hash = {}
const newArr = []
for (let index = 0; index < arr.length; index++) {
const element = arr[index]
const fieldVal = element[fieldId]
if (!hash[fieldVal]) {
hash[fieldVal] = true
newArr.push(element)
}
}
return newArr
}
// 深度拍平数组,深度扁平化数组
export function arrflattenDeep (arr) {
while (arr.some((item) => Array.isArray(item))) {
arr = [].concat(...arr)
}
return arr
}
/**
* 判断数组对象,是否有重复项
* arr数组对象
* fieldId:以此属性值作为是否重复判断依据
* [{fieldId:1,name:a,...},{fieldId:2, name:b,...},...]
*/
export function hasDuplicateArrObj (arr, fieldId) {
if (!Array.isArray(arr)) {
console.error('arr参数必须是一个数组,将原样返回输入参数')
return arr
}
// 不提供去重id则直接返回原数组
if (typeof fieldId !== 'string' || !fieldId) {
console.error('fieldId没有提供,将原样返回输入参数')
return arr
}
const flatArr = arrflattenDeep(arr)
const hash = {}
for (let index = 0; index < flatArr.length; index++) {
const element = flatArr[index]
const fieldVal = element[fieldId]
if (!hash[fieldVal]) {
hash[fieldVal] = true
} else {
// 出现重复项了
return true
}
}
return false
}
/**
* arr: 待去重数组,数组项必须是非引用类型,可以是string,number等类型
*/
export function uniqueArr (arr) {
if (!Array.isArray(arr)) {
console.error('arr参数必须是一个数组,将原样返回输入参数')
return arr
}
return [...new Set(arr)]
}
// 数组随机乱序,数组乱序
Array.prototype.shuffle = function () {
// var result = [],
// arr = this,
// len = arr.length
/*for (var i = 0; i < len; i++) {
var t = Math.floor(Math.random() * arr.length);
result.push(arr[t]);
var left = arr.slice(0, t),
right = arr.slice(t + 1, arr.length);
arr = left;
arr.push.apply(arr, right);
}*/
// for (var i = 0; i < len; i++) {
// var t = Math.floor(Math.random() * arr.length)
// result.push(arr.splice(t, 1)[0])
// }
// return result
let j
let i = this.length
while (i > 0) {
j = parseInt(Math.random() * i)
i--;
[this[j], this[i]] = [this[i], this[j]]
}
return this
}
// 带权重的数组乱序,格式[{value:"val1", priority:1},{value:"val2", priority:9},...]
// priority的值在[0,1]之间,包括0,小于1
Array.prototype.shuffleWithPriority = function () {
let prioritySum = this.reduce((total, elem) => total + elem.priority, 0)
let res = []
for (let i = 0; i < this.length; i++) {
const elem = this[i]
if (elem.priority / prioritySum > Math.random()) {
res.unshift(elem)
} else {
res.push(elem)
}
}
return res
}
// 从数组中随机提取一个元素
Array.prototype.random = function () {
const len = this.length
return this[parseInt(Math.random() * len)]
}
let arr = [1, 2, 3, 4, 5, 6, 7]
console.log(arr.shuffle())
console.log(arr.shuffle())
console.log(arr.random())
console.log([{ value: 1, priority: 1 }, { value: 2, priority: 10 }, { value: 3, priority: 20 }, { value: 4, priority: 30 }, { value: 5, priority: 50 }, { value: 6, priority: 60 }].shuffleWithPriority())