I am just starting to use associative arrays in JavaScript. I kind of understand that they are not really arrays, but they seem to serve my purpose, which is to load some configuration data from records into some session scope variables so I can very easily access them.
Now I have a data structure which can have multiple values for each key. I am having trouble loading this up.
Below is some pseudo-code. It runs through a loop, loading an associative array such that I will have the following:
key="App1" values="App1Title1,App1Title2,etc" key="App2" values="App2Title1,App2Title2,etc"
What is the best way to do this?
var tmpDes = {};
//Loop through data records
//Load the values below from loop, not hardcoded like here.
var keyStr:String = "key";
var valStr1:String = "value1";
var valStr2:String = "value2";
//This seems to work
tmpDes[tmpStr] = "xpApp1Title1";
tmpDes[tmpStr] = tmpDes[tmpStr] + "," + tmpStr1;
//
========================================================= These are excellent answers. I did realize from searching around that these are not really associative arrays but objects, I should of mentioned that.
Dan, the problem is I am in a loop through a table of docs, so I do not know valStr1 and valStr2 when I am constructing the js object. What I want in essence is to use "push" but these are not really arrays, so that will not work.
===================================================
This is closer but the problem is that I do not know the values that will be coming from the documents, and I do not understand how to make the array name come from a variable.
Documents in file contain these fields
KEY VALUE App1 App1Title1 App1 App1Title2 App2 App2Title1 App2 App2Title2 etc.
I need to create a structure so that if I call App1 I can get a list ==> "App1Title1":"App1Title2"
So I create my object and temp string variables, then start reading docs.
Load temp variables from first doc.
Then I want to set the obj. But I want to do that dynamically.
so the line
obj.tmpKey.push(tmpVal)
should execute
obj.App1.push(AppTitle1).
but obviously how I have written it it won't work it loads tmpKey with tmpVal
Maybe I am using the wrong structure? I am open to changing it.
var obj = {};
var tmpKey:String;
var tmpVal:String
//Loop docs, getting values.
tmpKey = doc.getItemValueString("var1");
tmpVal = doc.getItemValueString("var2");
obj.tmpKey.push(tmpVal);
//Loop