Jump forward or backward through an array by an index based on an item already in the array.


Solves an annoying problem I've run into frequently with various cases where I want to get another item in an array based on some current value that I know is in the array.

npm install array-jumper
var jumper = require('array-jumper');
// our demo array 
var arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
// If non are specified jumper should return first 
jumper(arr); // returns 'a' 
// If no jump amount specified jumper should return 
// next item in array 
jumper(arr, 'a') // returns 'b' 
// If jump amount specified is in range, jump to item 
jumper(arr, 'a', 2) // returns 'c' 
// Should work for negative 
jumper(arr, 'c', -2) // returns 'a' 
// Should work for zero 
jumper(arr, 'a', 0) // returns 'a' 
// Should loop around if given positive jump number that 
// is out of range. 
jumper(arr, 'a', 10) // returns 'd' 
// Should loop around if given negative jump number that 
// is out of range 
jumper(arr, 'a', -10) // returns 'e' 
// should work for item not found with no jump value 
jumper(arr, 'z') // returns 'a' 
// should work for item not found with jump value 1 
jumper(arr, 'z', 1) // returns 'a' 
// should work for item not found with jump value 2 
jumper(arr, 'z', 2) // returns 'b' 

If you like this, follow @HenrikJoreteg on twitter.

Check out similar resources at:

And check out my book:

run them with npm test

read them here: test.js

  • 1.0.0: no longer throwing errors, and now handling missing args better
  • 0.0.1: initial release