我在JavaScript中有一个定义了这些值的数组:
var myStringArray = ["1","2","3","4","5","6","7","8","9","10"];
当我第一次调用函数时,我需要得到这个:
1
2
3
再次打电话给我需要:
4
5
6
再次打电话:
7
8
9
再次打电话:
10
1
2
再次致电:
3
4
5
等等。你得到了重点,显示了数组中的3个值,如果我们在数组的末尾,请从头开始阅读......我有一个具有远程控制功能的应用程序,并且具有向下和向上键。当用户按下向下按钮从数组中获取这些值时,如上例所示,如果用户按下向上按钮,则需要从示例返回...所以在循环中读取数组(最后,数组从头开始读取,但始终显示三个值。)
我尝试使用它:
var myStringArray = ["1","2","3","4","5","6","7","8","9","10"];
var arrayLength = myStringArray.length;
for (var i = 0; i < arrayLength; i++) {
if (i<(6)) {
console.log(myStringArray[i]);
}
}
但是下次我调用这段代码时,它会从数组的开头显示,而不是继续读取其他值...我需要第二个计数器吗?
答案 0 :(得分:9)
如果您在循环浏览原始数组时可以正常操作,可以splice
和concat
类似于下面(或者如果您需要保留原始数组,则可以使用数组的克隆阵列):
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));
&#13;
如果您想要双向(就是您所说的那个?),如评论中所述,您可以按照以下方式进行操作,然后让您能够前进或后退,并且可以灵活地执行此操作任意数字:
var myStringArray = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"];
var loopByX = function(x) {
var len = myStringArray.length;
// Ensure x is always valid but you can add any behaviour here in that case yourself. As an example I return an empty array.
if (Math.abs(x) > len) {
return [];
}
var y = x > 0 ? myStringArray.splice(0, x) : myStringArray.splice(len + x, len);
myStringArray = x > 0 ? myStringArray.concat(y) : y.concat(myStringArray);
return y;
}
console.log(loopByX(20)); // invalid number
console.log(loopByX(-20)); // invalid number
console.log(loopByX(-3));
console.log(loopByX(-6));
console.log(loopByX(3));
console.log(loopByX(4));
&#13;
答案 1 :(得分:4)
你可以采用一个切割三个元素的函数,如果不可能,它也会获取数组所需的第一个值。
function take3() {
var temp = array.slice(index, index += 3)
index %= array.length;
console.log(temp.concat(temp.length < 3 ? array.slice(0, index) : []).join(' '));
}
var array = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
index = 0;
<button onclick="take3()">take 3</button>
使用动态计数的映射。
function take(length) {
console.log(Array.from({ length }, _ => array[++index, index %= array.length]));
}
var array = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
index = -1;
<button onclick="take(3)">take 3</button>
答案 2 :(得分:2)
您的变量i
是for
循环的本地变量,这意味着每次循环启动时它基本上都会重置。首先,让变量i
为全局。
var i=0;
function employeeNames(){
var empList = ["1","2","3","4","5","6","7","8","9","10"];
var output = [];
var j=0;
while(j<3)
{
output.push(empList[i])
i=(i+1)%empList.length;
j++;
}
return output;
}
console.log(employeeNames());
console.log(employeeNames());
console.log(employeeNames());
console.log(employeeNames());
console.log(employeeNames());
答案 3 :(得分:1)
如果您希望以不可变的方式实现循环循环
function loopArray(arr, step=3) {
let i = 0;
return function inner() {
for (let j = 0; j < step; j++) {
console.log(arr[i]);
i = (i + 1) % arr.length;
}
};
}
const func = loopArray(["1","2","3","4","5","6","7","8","9","10"], 3);
func();
func();
func();
func();
func();
答案 4 :(得分:1)
@Igor Petev ,JavaScript的闭包是一个很好的概念,可以用来解决你的问题。
请阅读JavaScript's Closures - w3schools文章。它非常好而且很棒。
我使用了闭包的概念来解决这个问题。如果您不理解我的代码或与此问题相关的任何其他内容,请发表评论。
请查看以下代码。
var get3items = (function () {
var index = 0;
var myStringArray = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"];
var len = myStringArray.length
return function () {
for(var count = 0; count < 3; count += 1)
{
console.log(myStringArray[index]);
if(index == (len - 1))
{
index = 0;
}
else {
index += 1;
}
}
}
})();
get3items (); // First call
console.log()
get3items (); // Second call
console.log()
get3items (); // Third call
console.log()
get3items (); // Fourth call
console.log()
get3items (); // Fifth call
/*
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
*/
答案 5 :(得分:1)
具有生成器功能的奇特解决方案:
function* cycle(arr) {
let i=0;
while (true) {
yield arr[i++];
i %= arr.length;
}
}
function* chunksOf(n, iterable) {
let chunk = [];
for (const x of iterable) {
chunk.push(x)
if (chunk.length >= n) {
yield chunk;
chunk = [];
}
}
if (chunk.length > 0)
yield chunk;
}
function toFunction(iterator) {
return () => iterator.next().value;
}
var myStringArray = ["1","2","3","4","5","6","7","8","9","10"];
const f = toFunction(chunksOf(3, cycle(myStringArray)));
console.log(f());
console.log(f());
console.log(f());
// …
答案 6 :(得分:0)
如何使用发电机:
function* get3() {
var myStringArray = ["1","2","3","4","5","6","7","8","9","10"];
var index = 0;
while (true) {
yield [0, 1, 2].map(i => myStringArray[(index + i) % myStringArray.length])
index = (index + 3) % myStringArray.length;
}
}
调用此函数会返回一个可以调用.next()
的对象,以获取下一组3:
var getter = get3();
console.log(getter.next().value); // ["1","2","3"]
console.log(getter.next().value); // ["4","5","6"]
console.log(getter.next().value); // ["7","8","9"]
// etc.
答案 7 :(得分:-1)
您需要一个额外的指针,以识别数组中的位置。
从头开始,当你到达数组的末尾时,你可以使用模运算符。
var myStringArray = ["1","2","3","4","5","6","7","8","9","10"];
var arrayLength = myStringArray.length;
var arrayPointer = 0;
for (var i = 0; i < 3; i++) {
console.log(myStringArray[arrayPointer++]);
arrayPointer %= arrayLength; // same as: arrayPointer = arrayPointer % arrayLength;
}
看看Akshay Garg的答案,看看如何将它包装在一个函数中。
答案 8 :(得分:-2)
function* employeeNames(){
var empList = ["1","2","3","4","5","6","7","8","9","10"];
for(var i =0; i<=empList.length; i++){
yield empList[i];
}
}
var emp;
emp = employeeNames();
它使用生成器功能......