So I have an array something like this:
var first_array = ['foo','bar','foobar'];
I am running a click function and trying to get the name of the array and loop through the array which has first as the ID name something like this
$('element').on('click',function(){
var id = $(this).attr('id');
var arr = id+"_array";
$.each(arr,function(index,value){
console.log(value);
})
})
Now the arr gives a variable name first_array and not the array. Hence the each loop fails. Is there a way to reference the array? I need to dynamically create the array variable name and get the array elements. I have also tried declaring the array globally and inside the click function but does not work.
first_arraywill be a string... You should use t as a key of the object...var obj = {first_array:['foo','bar','foobar']};objlikevar obj = {first_array:['foo','bar','foobar'],second_array:['bar','foo']}. Will try it out.window[first_array]wherefirst_arrayis a dynamic variable holding the array name.