Skip to content
Advertisement

Associative arrays in javascript

I have this object:

JavaScript

Its used in this way:

JavaScript

The problem is, in the addField() function the fields array is being set correct (perhaps because a numerical index is being used to refer to it) but the other 2 arrays (labels and rules) aren’t being touched at all. Doing a console.log shows them as empty in firebug.

What do I need to change to make them work? I’d still like to refer to the rules and labels by the string index of the field.

Advertisement

Answer

Use objects instead:

JavaScript

But as Christoph already mentioned, I would store this information in a single data structure too. For example:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement