0

Ideally my goal is to have a multi-dimensioned associative array. However it is my understanding that javascript doesn't really do associative arrays.

The data I want to store is a list of 4 digit codes associated with building names. Each building may have a different number of codes. I would like all of the data in one array.

This is the "sudo code", if you will, for how I would like it to come together:

var buildingZaps = [];

buildingZaps['Tall Tower'][] = '3421';
buildingZaps['Tall Tower'][] = '6874';
buildingZaps['Horrendous Hut'][] = '1387';
buildingZaps['Horrendous Hut'][] = '0844';
buildingZaps['Horrendous Hut'][] = '0098';
buildingZaps['Skimpy Skyscraper'][] = '9247';

I tried to do something a little less direct, since the above gave me an error:

var buildingIndexes = [];
var buildingZaps = [];

buildingIndexes['Tall Tower'] = 0;
buildingZaps[0][] = '3421';
buildingZaps[0][] = '6874';

buildingIndexes['Horrendous Hut'] = 1;
buildingZaps[1][] = '1387';
buildingZaps[1][] = '0844';
buildingZaps[1][] = '0098';

buildingIndexes['Skimpy Skyscraper'] = 2;
buildingZaps[2][] = '9247';

This doesn't seem to work either.

Does anyone know a way to get something close to the data structure I am looking for?

Thanks,
David

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by nizuya
0

I think the problem you are facing is the syntax. When you declare/initiate an empty array object in javascript, it would be as followed:

var anArray = new Array();

But if you want to use a hash, you can simply call it as followed:

var anObj = new Object();

anObj["a"] = "a value"
anObj["b"] = "another value"

And if you want a hash of array, you may do as followed:

var anObj = new Object();

anObj["a"] = ["val1", "val2", "val3"]
anObj["b"] = ["val4", "val5"]

Hope this help.

Edited by Taywin: n/a

0

It ended up being a much simpler syntax error. The build/code data queried from a data base and contained am '. This of course caused an error in defining the associative arrays.

The final code that works is as follows:

var buildingZaps = [];

buildingZaps["Taubman Center"] = ['0033', '0330', '0340', '5329', '5329', '5338'];
buildingZaps["UH"] = ['0010', '0018', '0030', '0032', '0117'];

Thanks for the help everyone.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.