Using Array object as variable name

Posted on 2014-08-09
Last Modified: 2014-08-18
I am looping through a large array and the value in one of the array objects will be the name of another variable on the page. I need to be able to write the array object but have the value processed as a variable name to pull the variables value.

Is this possible? If so how would I go about accomplishing it?
var Date17000 = "Jan 2014"
var Date23000 = "Feb 2014"

var projectsArray =


 { "Fund": "Project A",

  "Designation": "17000",

  "YearDes": Date17000,

  "Title": "General",

  "AmountAsked": "0.00",

  "UpdateSent": "",

 { "Fund": "Project B",

  "Designation": "23000",

  "YearDes": Date23000,

  "Title": "Restricted",

  "AmountAsked": "460.00",

  "UpdateSent": "",

   for(var i=0;i<projectsArray.length;i++) {

    var obj = projectsArray[i];
if (obj.YearDes == "Jan 2014") { var Jan2014 = Jan2014 + 1;
else if (obj.YearDes == "Feb 2014") { var Feb2014 = Feb2014 + 1;}

Open in new window

Question by:turtleman2009
    LVL 35

    Expert Comment

    by:Robert Schutt
    Your end goal is not entirely clear to me but the bit at the end there seems to suggest you just want to count all occurrences of months in the projects array. This may just do what you want:
      var arrCount = new Array();
      for(var i=0;i<projectsArray.length;i++) {
        var obj = projectsArray[i];
        if (!arrCount[obj.YearDes]) {
      // show specific result
      // show all results in an alert
      var msg = "";
      for(var yr in arrCount) {
        msg += yr + ": " + arrCount[yr] + "\n";

    Open in new window


    Author Comment

    I probably didn't explain it well. Sorry about that.

    The problem is I have an array that has all the information for individual product entries, the only thing it does not have included is the date. The dates are exported into another file as variables. But I need to be able associate each array entry with it's appropriate date from the list of variables so I can produce monthly and yearly reports about the amount of items as well as combined profit from each type of item.
    LVL 35

    Assisted Solution

    by:Robert Schutt
    Ok, I'm afraid in that case it's still not clear to me what you need to find and where you need to put it.

    Are you aware that the Datexxx variables are not actually put in the array, but their values are?

    Here are some alerts to show how you can combine info in the projects array to see what's in there, maybe it helps you to find or at least describe what you actually need to do.
      alert(projectsArray[0]["Fund"]);                       // => Project A
      alert(projectsArray[0]["Designation"]);                // => 17000
      alert(window["Date"+projectsArray[0]["Designation"]]); // => Jan 2014
      alert(projectsArray[0]["YearDes"]);                    // => Jan 2014

    Open in new window


    Accepted Solution

    I ended up fixing my issues a different way.

    Author Closing Comment

    Thanks for your help

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    In my daily work (mainly using, I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
    Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now