rkaartikeyan
rkaartikeyan

Reputation: 2007

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

In this Post How to move to prev/next element of an array its telling how to move prev/next a array ellement.

But i want the same concept for Prev/Next Array Element Key.

Ex:

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

Upvotes: 2

Views: 2192

Answers (4)

Nathaniel Johnson
Nathaniel Johnson

Reputation: 4849

Here is a plunker to go with the answer: http://plnkr.co/edit/x6stJnXzWzwY197csTHB?p=preview

If you have an array of items. The first element is always in the 0 position and the last element is in the array.length-1 position. Adding an element to an array is as simple as

arr.push("January");

Lets say you have a bunch of stuff in an array and you want to cycle through it by fixed amounts. Not necessarily 1, maybe 4. You also want to come back around when you pass the end of the array.

The first issue is that the array is a fixed length and your number to cycle (offset) might exceed the length of the array.

The solution is to use the modulus operator % which returns remainder after division.

This means that ((current)+offset)%lengthOfArray will return the correct position.

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

The answer marked as correct on the question you referenced has a big flaw in it.

Array.prototype.current = 0;

means that all arrays will share the same value for current. It would be better to declare the array like this:

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

Full source code:

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)); 

Upvotes: 4

Alex Ivasyuv
Alex Ivasyuv

Reputation: 8844

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

allows you to move in bound of 0...n with positive or negative difference. For example

if we need to go through 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

Upvotes: 0

Frogmouth
Frogmouth

Reputation: 1818

Try this:

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);

in jsfiddle

Upvotes: 1

Louis Brunner
Louis Brunner

Reputation: 283

Using the other SO answer, here is some basic functions which do what you want :

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;
};

Indexes implicitely created have a value of undefined.

Upvotes: 1

Related Questions