Link to home
Start Free TrialLog in
Avatar of turtleman2009
turtleman2009

asked on

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

Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

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

Avatar of turtleman2009
turtleman2009

ASKER

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.
SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for your help