Javascript 如何移动到数组的上一个/下一个索引键
在这篇文章中,它讲述了如何移动数组元素的上一个/下一个 但是我希望对Prev/Next数组元素键使用相同的概念 例:Javascript 如何移动到数组的上一个/下一个索引键,javascript,jquery,arrays,Javascript,Jquery,Arrays,在这篇文章中,它讲述了如何移动数组元素的上一个/下一个 但是我希望对Prev/Next数组元素键使用相同的概念 例: 使用另一个SO答案,以下是一些基本函数,它们可以满足您的需要: Array.prototype.next = function(i) { do { if (this[++i] != undefined) { return i; } } while (i < this.length); return n
使用另一个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;
};
Array.prototype.next=函数(i){
做{
如果(此[++i]!=未定义){
返回i;
}
}而(i0);
返回null;
};
隐式创建的索引的值为
undefined
,下面是答案:
如果您有一个项目数组。第一个元素始终位于0
位置,最后一个元素位于数组。length-1
位置。向数组中添加元素非常简单
arr.push("January");
假设你在一个数组中有一堆东西,你想按固定的数量循环。不一定是1,也许是4。当您通过阵列的末端时,您还希望返回
第一个问题是数组的长度是固定的,循环数(偏移量)可能超过数组的长度
解决方案是使用模数运算符%
,它返回除法后的余数
这意味着((当前)+偏移量)%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);
在
允许您在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
javascript中没有关联数组…数组没有索引键。我想您没有意识到显示的数组长度为13。请更详细地解释使用说明charlietf。创建arr[2]时,会隐式创建arr[0]和arr[1]。请传递一个参数:
…prototype.next=function(x){…}
,然后设置this.current=x
。在现实生活中,你还必须检查x
是否真的给出了等。感谢朋友们的快速回复。现在可以了。谢谢你的回答。这将返回元素值,我需要它返回元素键。不过还是谢谢你抽出时间。嗨,:)很高兴。。。所以请看评论。。。对于return键,您只需要更改以下行:if(typeof this[n]!==“undefined”)return this[n]
intoif(此[n]的类型)!==“未定义”)返回n代码>-此[n]代码>是数组元素值<代码>n
是元素值的键。
const calcIndex = (max, diff) => {
const limit = max + 1;
return (limit + diff) % limit;
}
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