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

Recommended Answers

All 3 Replies

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.

Is php/mySQL an option?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.