在JavaScript中循环读取数组值 [英] Read array values in a loop in JavaScript

查看:110
本文介绍了在JavaScript中循环读取数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JavaScript中有一个定义了这些值的数组:

I have an array in JavaScript that have defined these values:

var myStringArray = ["1","2","3","4","5","6","7","8","9","10"];

当我第一次调用函数时,我需要得到这个:

And when I call a function the first time function, I need to get this:

1
2
3

再次致电我需要:

4
5
6

再次拨打电话:

7
8
9

再次拨打电话:

10
1
2

再次致电:

3
4
5

依此类推。你得到了重点,显示了数组中的3个值,如果我们在数组的末尾,请从头开始阅读......我有一个具有远程控制功能的应用程序,并且具有向下和向上键。当用户按下向下按钮从数组中获取这些值时,如上例所示,如果用户按下向上按钮,则需要从示例返回...所以在循环中读取数组(最后,数组从头开始读取,但总是显示三个值。)

And so on. You got the point, showing 3 values from the array and if we are at the end of array, read from the beginning... I have an app that has remote control and has down and up keys. When the user presses the down button to get these values from an array as described in the above example, if the user presses the up button it needs to go back from an example...so reading the array in a loop (at end, the array is read from the beginning, but always shows three values).

我尝试使用它:

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]);
    }
}

但下次我调用此代码时,会显示从数组的起始值开始,不继续读取其他值...我需要第二个计数器吗?

But the next time I call this code, it shows from the beginning values of the array, not continue to read others value...do I need a second counter?

推荐答案

如果您可以在循环浏览原始数组时使用 splice concat 类似于下面(或者你)如果你需要保留原始数组,可以使用数组的克隆):

If you are OK with manipulating your original array as you loop through it you could splice and concat similar to below (or you could use a clone of the array if you need to persist the original array):

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));

如果你想要双向(是你所说的那个?),正如评论中所提到的,你可以按照下面的说法进行操作,然后让你能够向后或向前进行,并且可以灵活地以任意数字进行:

If you want to go bi-directional (is that what you call it?), as mentioned in the comments, you could do it as below which then gives you the ability to go backwards or forward and the flexibility to do so in an arbitrary number:

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));

这篇关于在JavaScript中循环读取数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆