[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 138
  • Last Modified:

Using Array object as variable name

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

0
turtleman2009
Asked:
turtleman2009
  • 3
  • 2
2 Solutions
 
Robert SchuttSoftware EngineerCommented:
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]) {
      arrCount[obj.YearDes]=0;
    }
    arrCount[obj.YearDes]++;
  }

  // show specific result
  alert(arrCount[Date17000]);

  // show all results in an alert
  var msg = "";
  for(var yr in arrCount) {
    msg += yr + ": " + arrCount[yr] + "\n";
  }
  alert(msg);

Open in new window

0
 
turtleman2009Author Commented:
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.
0
 
Robert SchuttSoftware EngineerCommented:
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

0
 
turtleman2009Author Commented:
I ended up fixing my issues a different way.
0
 
turtleman2009Author Commented:
Thanks for your help
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now