Reputation: 497
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?
Upvotes: 0
Views: 56
Reputation: 4217
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];
Upvotes: 1
Reputation: 115242
Simply get value from array using bracket notation
var goBack = userBack2[place - 1];
Upvotes: 1