70494

Dynamically referencing Javascript Array name without using Eval?

Question:

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.

Answer1:

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

window[calendarObject.id + '7']

Recommend

  • Kentico Repeater HTML Properties showing with selected transformation
  • How to change display text in item template based on eval in gridview?
  • JQM (jQueryMobile) Problem with AJAX content and focus()
  • Entity Framework ObjectContext: Concurrency
  • Using MouseListener to select a range of cells in a grid
  • Dart - Isolate Cross Window Communication
  • Access variable of ScriptContext using Nashorn JavaScript Engine (Java 8)
  • jQuery ready not fired after rails link_to is clicked
  • Jackson Parser: ignore deserializing for type mismatch
  • Allowing both email and username for authentication
  • Yii2: Config params vs. const/define
  • Algorithm for a smudge tool?
  • Initializer list vs. initialization method
  • Get one-time binding to work for ng-if
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Date difference with leap year
  • Websockets service method fails during R startup
  • How do you troubleshoot character encoding problems?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • AT Commands to Send SMS not working in Windows 8.1
  • Comma separated Values
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Benchmarking RAM performance - UWP and C#
  • Error creating VM instance in Google Compute Engine
  • Acquiring multiple attributes from .xml file in c#
  • Hits per day in Google Big Query
  • Angular 2 constructor injection vs direct access
  • how does django model after text[] in postgresql [duplicate]
  • How do I configure my settings file to work with unit tests?
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • How can I remove ASP.NET Designer.cs files?
  • Is it possible to post an object from jquery to bottle.py?
  • Django query for large number of relationships
  • Busy indicator not showing up in wpf window [duplicate]
  • Python/Django TangoWithDjango Models and Databases
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF