Skip to content

Instantly share code, notes, and snippets.

@Sigmus
Created February 7, 2014 15:15
Show Gist options
  • Save Sigmus/8864610 to your computer and use it in GitHub Desktop.
Save Sigmus/8864610 to your computer and use it in GitHub Desktop.
Returns next array item. Circular.
var nextArrayItem = function(arr) {
var len = arr.length;
return function(current) {
var currentIndex = arr.indexOf(current);
return currentIndex + 1 === len
? arr[0] : arr[currentIndex + 1]
};
}
var nextStatus = nextArrayItem(['fred', 'angela', 'mark']);
nextStatus('fred') // angela
nextStatus('angela') // mark
nextStatus('mark') // fred
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment