gpt4 book ai didi

javascript - 用于从 javascript 中的数组获取所有唯一对、三重音等的通用函数

转载 作者:行者123 更新时间:2023-11-30 16:47:39 27 4
gpt4 key购买 nike

我想在 javascript 中创建一个函数,它允许我传递一个长数组和一个参数。我正在寻找的是这样的东西:

var ar = [1,2,3,4];
var pairs = superAwesomeFunction(ar,2) //=> [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]];
var trebles = superAwesomeFunction(ar,3) //=> [[1,2,3],[1,2,4],[1,3,4],[2,3,4]

理想情况下,该函数对折叠参数没有限制。我写了一段看起来像这样的代码,它工作正常,但它并不是真正有用,因为它不是通用的,我需要很多这样的功能,这看起来很愚蠢。

  getAll2Folds: function (ar) {
var combinations = [],
numOdds = ar.length;
for (var i = 0; i < numOdds; i++) {
for (var j = i + 1; j < numOdds; j++) {
combinations.push([ar[i], ar[j]]);
}
}
return combinations;
},

getAll3Folds: function (ar) {
var combinations = [],
numOdds = ar.length;
for (var i = 0; i < numOdds; i++) {
for (var j = i + 1; j < numOdds; j++) {
for (var k = j + 1; k < numOdds; k++) {
combinations.push([ar[i], ar[j], ar[k]]);
}
}
}
return combinations;
};

},

...(不太好:|)

  getAll8Folds: function (ar) {
var combinations = [],
numOdds = ar.length;
for (var i = 0; i < numOdds; i++) {
for (var j = i + 1; j < numOdds; j++) {
for (var k = j + 1; k < numOdds; k++) {
for (var l = k + 1; l < numOdds; l++) {
for (var m = l + 1; m < numOdds; m++) {
for (var n = m + 1; n < numOdds; n++) {
for (var o = n + 1; o < numOdds; o++) {
for (var p = o + 1; p < numOdds; p++) {
combinations.push([ar[i], ar[j], ar[k], ar[l], ar[m], ar[n], ar[o], ar[p]]);
}
}
}
}
}
}
}
}
return combinations;

我可以自由使用下划线、jquery 或任何我想要的工具,但找不到一个优雅的解决方案,这也将是高性能的。想法?

谢谢

最佳答案

基本上 combine() 接受一个数组,其中包含要组合的值和所需组合结果集的大小。

内部函数c() 将一个先前组合的数组和一个起始值作为组合原始数组的索引。返回的是一个包含所有组合的数组。

第一次调用总是 c([], 0),因为结果数组为空且起始索引为 0。

var arr = [1, 2, 3, 4, 5, 6, 7];

function combine(a, size) {

function c(part, start) {
var result = [], i, l, p;
for (i = start, l = arr.length; i < l; i++) {
// get a copy of part
p = part.slice(0);
// add the iterated element to p
p.push(a[i]);
// test if recursion can go on
if (p.length < size) {
// true: call c again and concat it to the result
result = result.concat(c(p, i + 1));
} else {
// false: push p to the result, stop recursion
result.push(p);
}
}
return result;
}

return c([], 0);
}

out(JSON.stringify(combine(arr, 2), null, 4), true);
out(JSON.stringify(combine(arr, 3), null, 4), true);
out(JSON.stringify(combine(arr, 4), null, 4), true);
out(JSON.stringify(combine(arr, 5), null, 4), true);
out(JSON.stringify(combine(arr, 6), null, 4), true);

// just for displaying the result
function out(s, pre) {
var descriptionNode = document.createElement('div');
descriptionNode.className = 'description';
if (pre) {
var preNode = document.createElement('pre');
preNode.innerHTML = s + '<br>';
descriptionNode.appendChild(preNode);
} else {
descriptionNode.innerHTML = s + '<br>';
}
document.getElementById('out').appendChild(descriptionNode);
}
<div id="out"></div>

关于javascript - 用于从 javascript 中的数组获取所有唯一对、三重音等的通用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30998921/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com