如何在Javascript中“循环”关联数组的索引

时间:2021-04-09 21:30:30

This is a toggle with 2 or more items, where the next item needs to be picked so that all values will be picked in turn.

这是一个包含2个或更多项目的切换,需要选择下一个项目,以便依次选择所有值。

The indexes in this particular associative array are unique, their number is variable (2 or more), and there are no prototype elements. I know the old index, and I want to set the new index to the next one found (or the first one if the old one happens to be the last).

此特定关联数组中的索引是唯一的,它们的数量是可变的(2或更多),并且没有原型元素。我知道旧索引,我想将新索引设置为找到的下一个索引(如果旧索引恰好是最后一个索引,则设置第一个索引)。

This is what I came up with, but I was wondering if there is a more elegant way. The associative array is strings and the old index is oldx:

这就是我想出来的,但我想知道是否有更优雅的方式。关联数组是字符串,旧索引是oldx:

var i, 
    seen_oldx = 0, 
    newx = "";

for (i in strings) {
  if (seen_oldx) {
    newx = i; // index after oldx (not reached if oldx is last)
    break; // found it, stop looking
  }
  else if (i === oldx) 
    seen_oldx = 1;
  else if (!newx) 
    newx = i; // newx is the first (in case oldx is last)
  }
}

3 个解决方案

#1


0  

It would appear you are describing a stack. So you just pop the items off the stack as you need and there is no need for index, etc.

看起来你正在描述一个堆栈。所以你只需要根据需要弹出堆栈中的项目,而不需要索引等。

This is LIFO - last in first out

这是LIFO - 最后一次出局

 var stack=[];
 stack.push(1);
 stack.push(2);
 stack.push(3);

Then to remove items

然后删除项目

 var result = stack.pop();

#2


0  

This is a proposal with an array for the data and a property of the array array.index for keeping the last index and a function for returning the next element of the array.

这是一个提议,包含数据数组和数组array.index的属性,用于保存最后一个索引和一个返回数组下一个元素的函数。

var array = ['yellow', 'green', 'blue', 'orange'],
    i;

function getNext(array) {
    if (!('index' in array)) {                // test if property index exist
        array.index = -1;                     // if not create one with start value -1
    }
    array.index++;                            // increment index
    array.index = array.index % array.length; // move the index into the right interval
    return array[array.index];                // return element
}

for (i = 0; i < 10; i++) {
    document.write(getNext(array) + '<br>');
}

#3


0  

There doesn't seem a really elegant solution without adding another little data structure. I ended up adding a proper array of all the indexes of the strings associative array:

没有添加另一个小数据结构,似乎没有一个非常优雅的解决方案。我最终添加了字符串关联数组的所有索引的正确数组:

for (var i in strings) i_strings.push(i);

And then the code was just:

然后代码只是:

newx = (oldx + 1) % i_strings.length;

#1


0  

It would appear you are describing a stack. So you just pop the items off the stack as you need and there is no need for index, etc.

看起来你正在描述一个堆栈。所以你只需要根据需要弹出堆栈中的项目,而不需要索引等。

This is LIFO - last in first out

这是LIFO - 最后一次出局

 var stack=[];
 stack.push(1);
 stack.push(2);
 stack.push(3);

Then to remove items

然后删除项目

 var result = stack.pop();

#2


0  

This is a proposal with an array for the data and a property of the array array.index for keeping the last index and a function for returning the next element of the array.

这是一个提议,包含数据数组和数组array.index的属性,用于保存最后一个索引和一个返回数组下一个元素的函数。

var array = ['yellow', 'green', 'blue', 'orange'],
    i;

function getNext(array) {
    if (!('index' in array)) {                // test if property index exist
        array.index = -1;                     // if not create one with start value -1
    }
    array.index++;                            // increment index
    array.index = array.index % array.length; // move the index into the right interval
    return array[array.index];                // return element
}

for (i = 0; i < 10; i++) {
    document.write(getNext(array) + '<br>');
}

#3


0  

There doesn't seem a really elegant solution without adding another little data structure. I ended up adding a proper array of all the indexes of the strings associative array:

没有添加另一个小数据结构,似乎没有一个非常优雅的解决方案。我最终添加了字符串关联数组的所有索引的正确数组:

for (var i in strings) i_strings.push(i);

And then the code was just:

然后代码只是:

newx = (oldx + 1) % i_strings.length;