0

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.

1 Answer 1

5

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

window[calendarObject.id + '7']
Sign up to request clarification or add additional context in comments.

3 Comments

I still can't work out what the OP wants, but I think you're closer
Better to rewrite it so it is a property of a different object rather than leaving it as a global though.
@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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.