views:

537

answers:

1

Given that EVAL is Evil how do I create an Array name dynamically:

I have a bunch of Arrays and I need to reference different ones depending on what the user clicks.

This bit of code gives me the array object:

(eval(calendarObject.id + '7'))

But eval is bad, so how to do I construct an Array name and then reference it?

Here's a bit more context:

if (jQuery.inArray(String(checkinDate.getTime()/1000), 
(eval(calendarObject.id + '7'))) == -1 ) { //do stuff };

Any ideas?

thanks.

+4  A: 

It it's aglobal variable, it will be a property of the window object:

window[calendarObject.id + '7']
Greg
I still can't work out what the OP wants, but I think you're closer
annakata
Better to rewrite it so it is a property of a different object rather than leaving it as a global though.
David Dorward
@annakata - when my page is built via PHP it writes out a set of Arrays (ctg7, gdn7, twr7 and so on) I then need to access values from these depending on what the user clicks in the browser. Anyways, @Greg had the answer. Thank you very much :)@David Dorward - yes, I think I will, but it's working for now :)
firecall