集合运算
交集

交集 - intersect(arr)

功能:获取当前数组与目标数组的共有元素。

参数

  • {Array} arr - 对比数组

返回值

  • {Array} - 交集结果
1
2
3
4
5
Array.prototype.intersect = function(arr) {
if (!Array.isArray(arr)) return [];
const set = new Set(arr);
return this.filter(x => set.has(x));
};

差集

差集 - diff(arr)

功能:获取当前数组中存在,但目标数组中不存在的元素。

参数

  • {Array} arr - 对比数组

返回值

  • {Array} - 差集结果
1
2
3
4
5
Array.prototype.diff = function(arr) {
if (!Array.isArray(arr)) return this.slice();
const set = new Set(arr);
return this.filter(x => !set.has(x));
};

并集

并集 - union(arr)

功能:合并两个数组并去重。

参数

  • {Array} arr - 需要合并的数组

返回值

  • {Array} - 并集结果
1
2
3
Array.prototype.union = function(arr) {
return [...new Set([...this, ...arr])];
};

重组与变形
打乱


打乱 - shuffle()

功能:原地打乱数组顺序(Fisher-Yates 算法)。

返回值

  • {Array} - 打乱后的数组
1
2
3
4
5
6
7
Array.prototype.shuffle = function() {
for (let i = this.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[this[i], this[j]] = [this[j], this[i]];
}
return this;
};

条件拆分


拆分 - partBy(func)

功能:将数组按条件函数拆分为两个数组。

参数

  • {Function} func - 判断函数

返回值

  • {[Array, Array]} - [满足条件的数组, 不满足条件的数组]
1
2
3
4
5
6
Array.prototype.partBy = function(func) {
return this.reduce((acc, elem) => {
acc[func(elem) ? 0 : 1].push(elem);
return acc;
}, [[], []]);
};

分块


分块 - chunk(size)

功能:将数组按指定大小拆分为多个数组。

参数

  • {number} size - 每块大小

返回值

  • {Array<Array>} - 分块后的数组
1
2
3
4
5
6
Array.prototype.chunk = function(size) {
return Array.from(
{ length: Math.ceil(this.length / size) },
(_, i) => this.slice(i * size, i * size + size)
);
};

扁平化


扁平化 - flatten(depth)

功能:展平数组。

参数

  • {number} [depth=Infinity] - 深度

返回值

  • {Array} - 展平后的数组
1
2
3
4
5
Array.prototype.flatten = function(depth = Infinity) {
return this.reduce((acc, elem) =>
acc.concat(Array.isArray(elem) && depth > 0 ? elem.flatten(depth - 1) : elem), []
);
};

统计与分组
计数


计数 - countBy(func)

  • 功能:根据函数对数组元素计数。
  • 参数{Function} [func=x=>x] - 映射函数。
  • 返回值{Object} - 每个 key 出现的次数。
1
2
3
4
5
6
7
Array.prototype.countBy = function(func = (x) => x) {
return this.reduce((acc, elem) => {
const key = func(elem);
acc[key] = (acc[key] || 0) + 1;
return acc;
}, {});
};

分组


分组 - groupBy(keyFunc)

功能:根据函数对数组元素分组。

参数

  • {Function} keyFunc - 生成 key 的函数

返回值

  • {Object} - 分组对象 { key: 元素数组 }
1
2
3
4
5
6
7
Array.prototype.groupBy = function(keyFunc) {
return this.reduce((acc, elem) => {
const key = keyFunc(elem);
(acc[key] || (acc[key] = [])).push(elem);
return acc;
}, {});
};

去重


字段去重 - uniqueBy(field)

功能:根据对象字段去重。

参数

  • {string} field - 对象字段名

返回值

  • {Array} - 去重后的数组
1
2
3
4
5
6
7
8
9
10
11
12
13
Array.prototype.uniqueBy = function(iteratee) {
const seen = new Set();
const isField = typeof iteratee === 'string';

return this.filter(item => {
const key = (isField && item !== null) ? item[iteratee] :
(typeof iteratee === 'function' ? iteratee(item) : item);

if (seen.has(key)) return false;
seen.add(key);
return true;
});
};

权重随机

权重随机 - weightedRandom(weights)

功能:根据权重随机选取数组元素。

参数

  • {Array<number>} weights - 权重数组

返回值

  • {*} - 随机选中的元素
1
2
3
4
5
6
7
8
9
10
11
12
13
Array.prototype.weightedRandom = function(weights) {
if (!Array.isArray(weights) || weights.length !== this.length) {
throw new Error('weights length mismatch');
}
const totalWeight = weights.reduce((acc, w) => acc + w, 0);
const random = Math.random() * totalWeight;
let sum = 0;
for (let i = 0; i < this.length; i++) {
sum += weights[i];
if (random <= sum) return this[i];
}
};

组合与排列
组合


组合 - combinations(s)

功能:返回数组所有长度为 s 的组合。

参数

  • {number} s - 组合长度

返回值

  • {Array<Array>} - 组合数组
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Array.prototype.combinations = function(s) {
const result = [];
const length = this.length;
if (s <= 0 || s > length) return result;
const indices = Array.from({ length: s }, (_, i) => i);
while (indices[0] <= length - s) {
result.push(indices.map(i => this[i]));
let i = s - 1;
while (i >= 0 && indices[i] === i + length - s) i--;
if (i < 0) break;
indices[i]++;
for (let j = i + 1; j < s; j++) indices[j] = indices[j - 1] + 1;
}
return result;
};

排列


全排列 - permute()

功能:返回数组的全排列(Heap 算法)。

返回值

  • {Array<Array>} - 全排列数组
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Array.prototype.permute = function() {
const result = [];
const arr = this.slice();
const n = arr.length;
if (n === 0) return [[]];

const c = new Array(n).fill(0);
result.push([...arr]);

let i = 1;
while (i < n) {
if (c[i] < i) {
const j = i % 2 === 0 ? 0 : c[i];
[arr[j], arr[i]] = [arr[i], arr[j]];
result.push([...arr]);
c[i]++;
i = 1;
} else {
c[i] = 0;
i++;
}
}
return result;
};

笛卡尔积


笛卡尔积 - cartesianProduct(…arrays)

功能:返回数组与其他数组的笛卡尔积。

参数

  • {...Array} arrays - 其他数组

返回值

  • {Array<Array>} - 笛卡尔积数组
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Array.prototype.cartesianProduct = function(...arrays) {
if (arrays.length === 0) return this.map(x => [x]);
return arrays.reduce((acc, curr) => {
if (curr.length === 0) return [];

const res = [];
for (let i = 0; i < acc.length; i++) {
for (let j = 0; j < curr.length; j++) {
res.push([...acc[i], curr[j]]);
}
}
return res;
}, this.map(x => [x]));
};
INDEX
ACCESS_GRANTED_BY_LIMPID