Created
February 7, 2014 15:15
-
-
Save Sigmus/8864610 to your computer and use it in GitHub Desktop.
Returns next array item. Circular.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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