associative arrays; sorting, counting

M

mk834tt

When you use an object as an assc array as in:

var aa = new Array();

aa["peanuts"] = 2.50;
aa["popcorn"] = 3.50;
....

Is there a way to determine the number of entries?
So far I count this way: for (var x in aa) num_in_aa++;

How do you sort these? Is there a way to sort within the for/in
construct, something like

for(var x in 'sort aa') ????

Thanks.
 
R

RobG

That's ok. I thought there might be some special construct to sort
it. Thanks.

Javascript object properties are not ordered. That is, they are not
guaranteed to be returned in any particular order although most
browsers will return them in the order they are added - BUT you can't
depend on that. Some (or at least certain versions of some) will sort
them alphabetically and others have quirks where certain properties of
particular objects are always returned first (or last, or whatever) if
they exist.

There is a sort method available for arrays, but it only sorts those
elements that have numeric property names. As DS pointed out,
javascript Array objects are just objects with a special length
property and some extra methods (sort, split, push, etc.).

If you want to sort the properties of an object, you could put their
names into an array and sort it. Then use the array as an index to
the object.
 
A

AKS

How do you sort these?

var aa = new Array();

aa['cola'] = 4.50;
aa['something else'] = 10.02;
aa['peanuts'] = 2.50;
aa['popcorn'] = 3.50;

var arr = [];

for (var key in aa) {
arr.push([key, aa[key]]);
};

var tmp = Array.prototype.toString;

Array.prototype.toString = function () {
return this[1];
};

arr.sort(function (x, y) { return x - y; });

Array.prototype.toString = tmp;

alert(arr.join('\n').replace(/,/g, ': '));
 
T

Thomas 'PointedEars' Lahn

When you use an object as an assc array as in:

var aa = new Array();

aa["peanuts"] = 2.50;
aa["popcorn"] = 3.50;
...

Is there a way to determine the number of entries?
So far I count this way: for (var x in aa) num_in_aa++;

How do you sort these? Is there a way to sort within the for/in
construct, something like

for(var x in 'sort aa') ????

JavaScript 1.7 as of Gecko 1.8.1 (e.g. in Firefox 2.0) introduces iterators
that allow you to do just that:

http://PointedEars.de/scripts/es-matrix/#i
http://developer.mozilla.org/en/docs/New_in_JavaScript_1.7#Iterators

Otherwise you should follow Rob's suggestion.

In addition, also because you are interested in "the number of entries", you
could construct and use a collection yourself, or use an existing
implementation, like <http://PointedEars.de/scripts/collection.js>. I have
not tested it sufficiently yet, so you might have to tweak it a bit -- any
feedback is welcome. (However, please also take note of the licensing
conditions under which it is distributed.)


HTH

PointedEars
 
D

dhtmlkitchen

Javascript object properties are not ordered.  That is, they are not
guaranteed to be returned in any particular order although most
browsers will return them in the order they are added - BUT you can't
depend on that.  Some (or at least certain versions of some) will sort
them alphabetically and others have quirks where certain properties of
particular objects are always returned first (or last, or whatever) if
they exist.

So there's some browser or version that sort properties alphabetically
and another that returns particular objects first.

I'm curious: Which browsers or versions?

ES4 is attempting to standardize that properties are returned in the
order in which they're defined, based on the premise that that is what
all impl's do.


Garrett
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top