0

I have 1 variable and 1 array. The array contains any number of numbers, from 0 to 10 - like such: 0,1,2,3,4,5 and so on.

My variable contains 1 number, let's say 4 - I want to use the variable to get the number at index 4 in the array, minus 1. So in the above examples, I would get 2.

How can I go about creating a variable with that number? I have tried:

var goBack = $(userBack2).index(place-1);

Where goBack is the variable I am creating, userBack2 is the array containing my numbers and place is my variable determining what index I need.

I know that the syntax is completely wrong, that index might not be correct to use here, but I simply cannot figure out how to do it. Do I need to use filter? if so, how?

2
  • alert(ar[variable - 1])? Commented Jun 19, 2016 at 18:41
  • var goBack = userBack2[place-1]; Commented Jun 19, 2016 at 18:42

2 Answers 2

1

Simply get value from array using bracket notation

var goBack = userBack2[place - 1];
Sign up to request clarification or add additional context in comments.

Comments

1

I think that you don't need to use jquery anymore. Native javascript is enough.

var items = [0, 1, 2, ..., 10];
var num = 4;
var goBack = items[num - 1];

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.