Jorge said:
If there was such an thing as an associative array in ECMAScript,
certainly you could. But what was meant here was a simple Object object,
and definitely one can iterate over (the properties of) such an object
(it is only a matter of how, see also my other posting).
When an object is expressed as a JSON text, the properties' names are
declared explicitly:
var object= {};
object['0']= 6;
object['1']= 6;
object['2']= 6;
object.x= "xxx"; -> JSON text '{ "0":6, "1":6, "2":6, "x":"xxx" }'
But when an Array is expressed as a JSON text (even though its indices
are in fact properties of an (array-like) Array-object) the indexes
aren't explicitly declared, and that's why non-numeric (Array) indexes
can't be expressed in a JSON text:
Only very rougly speaking. In fact, an array in JSON is written like an
ECMAScript Array initializer (‘[...]’) which only allows to add (not:
declare) properties with numeric name, in numeric ascending order, to
the newly constructed Array object.
If property addition order or property names are not to be numerical
ascending, JSON object syntax must be used instead which is close to
ECMAScript Object initializers (‘{...}’). (AFAIK, the only difference
between the former and the latter being that the former requires all
property names to be quoted, the latter only non-Identifiers.)
Those two ways can be combined easily. In this case, the following might
help:
{"a": [6, /5/, "4"], "x": "xxx"}
This, when parsed as an Object object referred to here as ‘o’, can be
iterated over as follows:
for (var p in o)
{
var v = o[p];
if (v.constructor == Array)
{
for (var i = 0, len = v.length; i < len; i++)
{
var e = v
;
// ...
}
}
else
{
// ...
}
}
This code is a stub. Generation of the string representation of this
object (and a uniform more object-oriented way to generate and return
the string representation of any Object or Array object) is left as an
exercise to the reader. Please observe the usual security precautions.
PointedEars