Javascript associative array initialization error?

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

results in an โ€œmissingโ€ XML expression with an arrow pointing to the first colon in the JS error console. Is this a valid declaration?

+3
source share
4 answers

You must fix the tag first <script>before

<script type="text/javascript">

Next, if you want to use numeric indices, try declaring them as a string:

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

Please note, however, that you cannot reference them in object notation (i.e. tids.308). You can simply use arrays instead of objects:

+8
source

This is not an associative array - it is just a JS object. I believe that you need to make key strings instead of numeric ones.

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

.

+1

, . ;

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

I tried in IE and FF, and the code is fine. This should be a mistake of other codes.

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

0
source

Source: https://habr.com/ru/post/1728071/


All Articles