user105033
user105033

Reputation: 19598

javascript associative array initialization error?

<script>
var tids = {
308: 1,
312: 1,
313: 1,
314: 1
};
</script>

results in "missing } in XML expression with an arrow pointing to the first colon in the JS error console. Isn't this a valid declaration?

Upvotes: 3

Views: 11267

Answers (4)

Billy
Billy

Reputation: 15706

I have tried in both IE and FF and the code is fine. It should be the error of other codes.

Please use Firefox Web Developer and Firebug to find the source of error.

Upvotes: 0

Brian Moeskau
Brian Moeskau

Reputation: 20431

This isn't an associative array -- it's just a JS object. I believe you need to make the keys strings instead of numeric.

var tids = {
"308": 1,
"312": 1,
"313": 1,
"314": 1
};

More info on associative arrays vs. regular objects.

Upvotes: 1

Corey Ballou
Corey Ballou

Reputation: 43507

First you should fix your <script> tag to

<script type="text/javascript">

Next, if you want to use numeric indexes, try to declare them as a string:

var tids = {
'308': 1,
'312': 1,
'313': 1,
'314': 1
};

Please note, however, that you will not be able to reference them in object notation (i.e. tids.308). You could simply use arrays instead of objects:

Upvotes: 8

returnvoid
returnvoid

Reputation: 434

i guess that the key cannot start with a number. try;

<script>
var tids = {
n308: 1,
n312: 1,
n313: 1,
n314: 1
};
</script>

Upvotes: 0

Related Questions