有没有办法循环使用此代码并更改数字(i = 1,2,3)以避免重复?
if (bool) {
reeks1Getallen = reeks1.splice(0, k1 % (reeks1.length));
reeks2Getallen = reeks2.splice(0, k2 % (reeks2.length));
reeks3Getallen = reeks3.splice(0, k3 % (reeks3.length));
} else {
reeks1Getallen = reeks1.splice(0, (reeks1.length) - k1 % reeks1.length);
reeks2Getallen = reeks2.splice(0, (reeks2.length) - k2 % reeks2.length);
reeks3Getallen = reeks3.splice(0, (reeks3.length) - k3 % reeks3.length);
}
reeks1 = reeks1.concat(reeks1Getallen);
reeks2 = reeks2.concat(reeks2Getallen);
reeks3 = reeks3.concat(reeks3Getallen);
答案 0 :(得分:2)
听听@Bergi所说的话;这看起来就像我的教授计划之一。
如果你现在无法摆脱枚举变量,那么你的代码应该等同于:
for (let [reeks, k] of [[reeks1, k1], [reeks2, k2], [reeks3, k3]]) {
const getallen = reeks.splice(0, bool ? k % reeks.length : reeks.length - k % reeks.length);
reeks = reeks.concat(getallen);
}
答案 1 :(得分:1)
如果要使用索引进行循环,则应使用对象数组。这个答案比其他答案更冗长,但它也应该完成工作:
var objList = [
{
reeks: [], // fill with your own array
reeksGetallen: [], // fill with your own array
k: 1
},
{
reeks: [], // fill with your own array
reeksGetallen: [], // fill with your own array
k: 2
},
{
reeks: [], // fill with your own array
reeksGetallen: [], // fill with your own array
k: 3
}
];
for (var i = 0; i < objList.length; i++) {
var reeksGetallen = objList[i].reeksGetallen;
var reeks = objList[i].reeks;
var k = objList[i].k;
if (bool) {
reeksGetallen = reeks.splice(0, k % reeks.length);
} else {
reeksGetallen = reeks.splice(0, reeks.length - k % reeks.length);
}
reeks = reeks.concat(reeksGetallen);
}