JavaScript 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
Answer:1

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

Answer:2

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)); 
Answer:3

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.

Answer:4

Im using jTable to display CDs info and a child table to show reviews of that CD. I want to be able to only show the edit\delete buttons on the rows for the user that is logged in. I have been trying ...

Im using jTable to display CDs info and a child table to show reviews of that CD. I want to be able to only show the edit\delete buttons on the rows for the user that is logged in. I have been trying ...

I need some help to fix an error on this code: var image = new Array (); image[0] = "http://placehold.it/20"; image[1] = "http://placehold.it/30"; image[2] = "http://placehold.it/40"; image[3] = "...

I need some help to fix an error on this code: var image = new Array (); image[0] = "http://placehold.it/20"; image[1] = "http://placehold.it/30"; image[2] = "http://placehold.it/40"; image[3] = "...

  1. random image on refresh website
  2. random image on refresh
  3. random image on refresh html code
  4. random image on refresh php
  5. random background image refresh
  6. html random image on refresh
  7. javascript random image on refresh
  8. random image every time you refresh

I have a pop-up with an onclick action to close the box when the submit button is pressed: <input type="submit" name="submit" value="submit" onclick="javascript: window.close();"> I'm trying ...

I have a pop-up with an onclick action to close the box when the submit button is pressed: <input type="submit" name="submit" value="submit" onclick="javascript: window.close();"> I'm trying ...

  1. javascript window close event
  2. javascript window close self
  3. javascript window close event example
  4. javascript window close print preview
  5. javascript window close event handler
  6. javascript window close not working
  7. javascript window close tab
  8. javascript window close event listener
  9. javascript window close
  10. javascript window close chrome
  11. javascript window.close not working in google chrome
  12. javascript window.close without prompt
  13. javascript window.close doesn't work
  14. javascript window.close not working in ie11
  15. javascript window close callback
  16. javascript window close after 5 seconds
  17. javascript window close event chrome
  18. javascript window.close not working in chrome
  19. javascript window close firefox
  20. javascript window close listener

I have a jsfiddle. obj = {}; obj[0] = 'hej'; obj[1] = 'hopp'; $.each( obj, function( key, value ) { console.log($.now()); }); $.now() is the same number in the console. How can I generate a ...

I have a jsfiddle. obj = {}; obj[0] = 'hej'; obj[1] = 'hopp'; $.each( obj, function( key, value ) { console.log($.now()); }); $.now() is the same number in the console. How can I generate a ...

  1. jquery unique array
  2. jquery unique id
  3. jquery uniquesort
  4. jquery unique values in array
  5. jquery unique array of objects
  6. jquery unique id generator
  7. jquery unique array of strings
  8. jquery unique not working
  9. jquery unique selector
  10. jquery unique validation
  11. jquery unique function
  12. jquery unique method
  13. jquery uniqueid is not a function
  14. jquery unique random number generator
  15. jquery unique select options
  16. jquery .uniqueid() example
  17. jquery unique id for element
  18. jquery unique example
  19. jquery unique json
  20. jquery unique list