Thursday, 15 July 2010

javascript - Access an Array whose name is partially a variable -



javascript - Access an Array whose name is partially a variable -

given

var wall_list = []; var granary_list = []; var building = 'granary';

how can access granary_list using var building, example:

$.each('' + building + '_list', function(j, v) { };

when seems attempts access granary_list, , fails, if console.log('' + building + '_list') logs granary_list - not array, word 'granary_list'.

any tips?

you can access global variables window object

window[building+'_list'];

accesses granary_list variable

javascript jquery arrays variables each

No comments:

Post a Comment