由于这个伟大的站点使用了来自Nope的优秀代码,我得到了以下代码:
var myStringArray = ["1","2","3","4","5","6","7","8","9","10"];
var loopByX = function(x){
var y = myStringArray.splice(0,x);
myStringArray = myStringArray.concat(y);
return y;
}
console.log(loopByX(3));
console.log(loopByX(3));
console.log(loopByX(3));
console.log(loopByX(3));
console.log(loopByX(3));这很好.我将它添加到HTML5应用程序中,并发现描述输出的例子有点错误,所以我需要得到这个输出:
1
2
3
2
3
4
3
4
5
4
5
6
5
6
7
6
7
8
7
8
9
8
9
10
9
10
1
10
1
2
1
2
3因此,为了得到值减去1或+1(按上或下按钮,因此称为双向),使用上面的代码,我得到3项向上或向下,我看到当我复制粘贴我需要的代码时,我需要在loop...if中得到一个项目,以修改代码来完成.我尝试在函数和y-1中做x-1,但是它不会给我上面的示例输出.
所以我需要一个函数,我可以调用loopByX(3)和多个调用函数,它将在循环中左移一个位置,调用多个函数,loopByX(-3),在loop...what中右移一个位置,需要修改以保存上面的输出?
非常感谢。
发布于 2018-05-22 19:29:57
您可以使用双数组和调整后的索引。
function take(direction) {
index += direction + array.length;
index %= array.length;
console.log(array.concat(array).slice(index, index + 3).join(' '));
}
var array = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
index = 0;<button onclick="take(-1)">-</button>
<button onclick="take(1)">+</button>
用<div> ... </div>
function take(direction) {
index += direction + array.length;
index %= array.length;
console.log(
array
.concat(array)
.slice(index, index + 3)
.map(v => `<div>${ v }</div>`)
.join('')
);
}
var array = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
index = 0;<button onclick="take(-1)">-</button>
<button onclick="take(1)">+</button>
https://stackoverflow.com/questions/50474996
复制相似问题