如何从顺时针和逆时针的某个位置循环数组?

时间:2016-05-07 20:50:58

标签: javascript arrays loops iteration

我有这个数组:

void Dialog_Chooser(GtkWidget *widget, gpointer gst)
{
  GtkWidget *dialog;
  GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN;
  GtkFileChooser *chooser;
  gint res;

  dialog = gtk_file_chooser_dialog_new("Open File", GTK_WINDOW(win), action, "Cancel",
                                       GTK_RESPONSE_CANCEL, "Open", GTK_RESPONSE_ACCEPT, NULL);

  gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);

  res = gtk_dialog_run(GTK_DIALOG(dialog));

  if(res == GTK_RESPONSE_ACCEPT){
    GSList *filenamepus;

    chooser = GTK_FILE_CHOOSER(dialog);

    //filename = gtk_file_chooser_get_filename(chooser);
    filenamepus = gtk_file_chooser_get_filenames(chooser);

    int    nIndex;
    GSList *node;

    for(nIndex = 0; node = g_slist_nth(filenamepus, nIndex); nIndex++){
      filename = (char *) node->data; //g_slist_nth(filenamepus, nIndex);
      Add_Items_List(NULL, NULL);
      //g_print ("%s\n", filename); //(char *) node->data);
    }

    //Add_Items_List(NULL, NULL);

    g_free(filename);
  }

  gtk_widget_destroy(dialog);
}

以顺时针方向循环(开始8,然后是9,然后是10,然后是0 ......)我这样做:

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

1)顺时针方向,还有更好的方法吗?

2)以逆时针方向循环(开始2,然后是1,然后是0,然后是10 ......),我该怎么办?

5 个答案:

答案 0 :(得分:2)

要做到与你所做的相似,从起始索引中减少索引并在修剪前添加长度:

var start = 8;
for(i = 0; i < array.length; i++) {
  index = (start - i + array.length) % array.length;
  // ....
}

关于“如何做得更好”,我将创建一个简单的辅助函数:

function getIndexInRange(index, length) {
  var trim = index % length;
  var nonNegative = trim + length;
  return nonNegative % length;
}

然后一切都变得更加清晰:

var start = 8;
for(i = 0; i < array.length; i++) {
  var index = getIndexInRange(start + i, array.length);
  // ....
}

for(i = 0; i < array.length; i++) {
  var index = getIndexInRange(start - i, array.length);
  // ....
}

现在你甚至可以根据需要多次迭代数组,它仍然有效:

for(i = 0; i < array.length * 5; i++) {
  var index = getIndexInRange(start - i, array.length);
  // ....
}

答案 1 :(得分:1)

我在这里创建了一个jsbin。

http://jsbin.com/tucusal/edit?html,js,console

您甚至可以创建一个接受方向输入的函数,然后沿该方向遍历数组。

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

var start = 8;
clockwise = 1;
anti_clockwise = -1;

direction = clockwise;
traverse(array, start, anti_clockwise);

function traverse(array, start,  direction) {
  var count = array.length;
  for (i = start; count > 0; i += direction) {
    var index = array[(array.length + i) % array.length];
    count--;
    console.log(index);
  }
}

答案 2 :(得分:1)

&#13;
&#13;
var array = [0,1,2,3,4,5,6,7,8,9,10];


function clockwise(start){
    for (var i = 0; i < array.length; i++){
        console.log((start+i)%array.length);
    }
}

function counterClockwise(start){
    for (var i = array.length; i > 0; i--){
        console.log((start+i)%array.length);
    }
}

console.log('clockwise start from ');
clockwise(8);
console.log('clockwise End  ');

console.log('counterClockwise start from ');
counterClockwise(2);
console.log('counterClockwise End  ');
&#13;
&#13;
&#13;

答案 3 :(得分:1)

考虑在两个方向使用单一功能:

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

function iterateByClockRotation(start, array, direction){
    var len = array.length, current = start;
    while (len--) {
        current = array.indexOf(current);
        if (current < 0) current = ((direction === "clockwise")? 0 : array.length-1);
        console.log(array[current]); // the current value
        (direction === "clockwise")? current++ : current--;
    }
}

iterateByClockRotation(8, array, "clockwise");

“顺时针”方向的输出:

8
9
10
0
1
2
3
4
5
6
7
iterateByClockRotation(2, array, "anticlockwise");

'逆时针'方向的输出:

2
1
0
10
9
8
7
6
5
4
3

答案 4 :(得分:1)

您可以使用Array.prototype.slice更改数组的开头:

Array.prototype.enhancedForEach = function(callback, start = 0, clockwise = true) {
  var array = this;
  start %= array.length;

  array.slice(start)
    .concat(array.slice(0, start))
    .forEach((v, i, arr) => {
      var index = clockwise ? i : arr.length - i - 1;
      callback(arr[index], index, arr);
    });

}

array = Array.from({
  length: 20
}, (v, i) => i);

array.enhancedForEach(v => console.log(v), 4);

array.enhancedForEach(v => console.log(v), 0, false);
http://stackoverflow.com/posts/37981887/edit#