0

I have an array setup like the following:

services = ['times', 'food', 'messages', 'share']

And I would like to be able to pass into services 'times' and have it return the next position in the array i.e. 'food'

0

2 Answers 2

1
nextPos = services.indexOf('times');

This is JS, but also valid in CoffeeScript. Note, though, that if used outside of CofeeScript, it's an ECMA5 addition, so won't work in older browsers (EDIT - as the comment says, this of course means IE <= 8).

Sign up to request clarification or add additional context in comments.

3 Comments

More specifically, it won't work in IE 8 and below. Reference
I would do nextPost = services.indexOf 'times' to make it more CS-esque
This is not valid CoffeeScript. var isn't a legal token.
1

Based on Utkanos answer but in CS:

getNext = (arr, key) ->
    arr[arr.indexOf(key) + 1]
console.log getNext services, 'times' # food

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.