Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a variable in array index

i repeat the title because everything is there : How to pass a variable in array index

var xyz = 0;
var somearray = ['a','b','c'];
var content = somearray[xyz]; - **that dont work !**

what should be the RIGHT way to do that ?

like image 959
menardmam Avatar asked Dec 07 '25 13:12

menardmam


1 Answers

Just a stab in the dark here, but perhaps the OP is using inArray and might be asking (indirectly) how to get the intellisense working in whatever tool they're using.

If that's the case, I'm sure someone here can provide a more elegant solution but something similar to the following should work:

var somearray = ['a','b','c'];
var index = $.inArray('a', somearray);
if (index > -1) {
    index = isNaN(index) ? 0 : index;
    var content = somearray[index];
}
like image 105
Oddlyeven Avatar answered Dec 09 '25 01:12

Oddlyeven