最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Loop seamlessly over an array, forwards or backwards given an offset larger than the array - Stack Overflow

programmeradmin1浏览0评论

This is a similar question to what I have asked before (Select X amount forward and backwards in an array, looping to beginning and end if needed)

But I'm having trouble adapting the answers to a different problem I'm trying to solve.

given an arbitrary array and a current index

[a, b, c, d, e, f, g, h, i, j, k]

Let's say the current index is 0 for now (a)

I need to find a new index, given an offset of n (let's say 30, it could also be negative to go backwards). Such that it would loop through the array, continuing from the beginning when it got to the end (or continuing from the end if you where looping backwards) and just return the new array index.

I've managed to adapt an answer from the similar question to walk the array forwards, but it breaks when I try changing it to walk backwards.

function crawlArrayForwards(array, index, n){
    var finalIndex;
    for (var i = index, len = array.length; i <= index + n; i++) {
        finalIndex = (i + len) % len;
    }
    return finalIndex;
}

This is a similar question to what I have asked before (Select X amount forward and backwards in an array, looping to beginning and end if needed)

But I'm having trouble adapting the answers to a different problem I'm trying to solve.

given an arbitrary array and a current index

[a, b, c, d, e, f, g, h, i, j, k]

Let's say the current index is 0 for now (a)

I need to find a new index, given an offset of n (let's say 30, it could also be negative to go backwards). Such that it would loop through the array, continuing from the beginning when it got to the end (or continuing from the end if you where looping backwards) and just return the new array index.

I've managed to adapt an answer from the similar question to walk the array forwards, but it breaks when I try changing it to walk backwards.

function crawlArrayForwards(array, index, n){
    var finalIndex;
    for (var i = index, len = array.length; i <= index + n; i++) {
        finalIndex = (i + len) % len;
    }
    return finalIndex;
}
Share Improve this question edited May 23, 2017 at 10:27 CommunityBot 11 silver badge asked Nov 15, 2013 at 11:19 user1622998user1622998
Add a ment  | 

3 Answers 3

Reset to default 10

Look, you don't need a for loop or anything. You just have to add your number with its sign and take the modulus.

function crawlArray(array, index, n) {
    return ((index + n) % array.length + array.length) % array.length;
}

And that's it. Should work with positive or negative values of n.

It's not exactly elegant, but if you have a working method for going forwards, you can just put an 'if' statement in at the start, check it's bigger than 0. If it isn't, you can just reverse everything, multiply the offset by -1, and do it anyway! :D

Well I if you have a as your current index and b as your target index (which might be bigger or smaller than the length or zero)

than

b2 = b % (list.length - 1)

delivers a valid index in the array.

if you than subtract b2 - a = d you know if how many step to go and also if up- or downwards, depending if d is bigger or smaller than zero

for(var i = a; i !== b2; i += (d > 0) ? 1 : -1) {

}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论