如何移动到数组的上一个/下一个索引键

How to move to prev/next index key of an array

本文关键字:上一个 下一个 索引 数组 何移动 移动      更新时间:2023-09-26

在这篇文章中,如何移动到数组的上一个/下一个元素,它告诉我们如何移动数组的上下一个。

但是我想要上一个/下一个数组元素键的相同概念。

例如:

var arr = new Array();
arr[2] = 'hi';
arr[3] = 'hello';
arr[4] = 'how are you';
arr[12] = 'i am fine';
arr.next(3); // returns 4
arr.next(4); // returns 12
arr.next(2); // returns 3
arr.next(12); // returns 2
arr.prev(2); // returns 12

这里有一个plunker来回答:http://plnkr.co/edit/x6stJnXzWzwY197csTHB?p=preview

如果您有一个项目数组。第一个元素总是在0位置,最后一个元素在array.length-1位置。向数组中添加元素就像一样简单

arr.push("January");

比方说,你在一个数组中有一堆东西,你想以固定的数量循环通过它。不一定是1,可能是4。当你经过数组的末尾时,你还想回来。

第一个问题是数组是固定长度的,要循环的数量(偏移量)可能超过数组的长度。

解决方案是使用模算子%,它在除法后返回余数。

这意味着((current)+offset)%lengthOfArray将返回正确的位置。

Array.prototype.move = function(i) {
  this.current = (this.current + i) % this.length;
  return this[this.current];
};

你提到的问题上标记为正确的答案有一个很大的缺陷

Array.prototype.current = 0;

意味着所有阵列将共享相同的电流值。最好这样声明数组:

var arr = []; //Creates an array
arr.current = 0;

完整源代码:

Array.prototype.move = function(i) {
  this.current = (this.current + i) % this.length;
  return this[this.current];
};

var arr = []; //Creates an array
arr.current = 0; // Better than overriding array constructor 

arr.push("January");
arr.push("February");
arr.push("March");
arr.push("April");
arr.push("May");
arr.push("June");
arr.push("July");
arr.push("August");
arr.push("September");
arr.push("October");
arr.push("November");
arr.push("December");

console.log(arr.move(1));
console.log(arr.move(1));
console.log(arr.move(1));
console.log(arr.move(1));
console.log(arr.move(4));
console.log(arr.move(12)); 

试试这个:

var arr = new Array();
arr[2] = 'due';
arr[3] = 'hello';
arr[4] = 'quattro';
arr[12] = 'i am fine';
Array.prototype.next = function(n,_k){
    if(typeof this !== "object") return false;
    _k = n;
    n = (typeof n === "undefined" || n+1 == this.length) ? 0 : n + 1;
    for(n; this.length > n+1; n++){
        if(typeof this[n] !== "undefined") return this[n]; // return element value if you want only index use "return n" ... this is for all return
    }
    for(n = 0;n == _k; n++){
        if(typeof this[n] !== "undefined") return this[n];
    }
    return false;
}
Array.prototype.prev = function(n,_k){
    if(typeof this !== "object") return false;
    _k = n;
    n = (typeof n === "undefined" || n == 0) ? this.length : n-1;
    for(n; n > 0; n--){
        if(typeof this[n] !== "undefined") return this[n];
    }
    for(n = 0;n == _k; n--){
        if(typeof this[n] !== "undefined") return this[n];
    }
    return false;
}
var el = arr.next(12);
var el2 = arr.prev(12);
console.log(el);
console.log(el2);

在jsfiddle 中

使用另一个SO答案,这里有一些基本函数可以实现您想要的功能:

Array.prototype.next = function(i) {
    do {
       if (this[++i] != undefined) {
           return i;
       }
    } while (i < this.length);
    return null;
};
Array.prototype.prev = function(i) {
    do {
       if (this[--i] != undefined) {
           return i;
       }
    } while (i > 0);
    return null;
};

隐含创建的索引的值为undefined

   const calcIndex = (max, diff) => {
     const limit = max + 1;
     return (limit + diff) % limit;
   }

允许您在0…n的范围内移动,具有正差或负差。例如

如果我们需要通过0…4:

calcIndex(4, 0) => 0
calcIndex(4, 1) => 1
calcIndex(4, 2) => 2
calcIndex(4, 3) => 3
calcIndex(4, 4) => 4
calcIndex(4, 5) => 0
calcIndex(4, 6) => 1
calcIndex(4, -1) => 4
calcIndex(4, -2) => 3