473,406 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

associative arrays; sorting, counting

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.

Dec 20 '07 #1
4 1549
DS
On Wed, 19 Dec 2007 19:47:19 -0800, mk834tt wrote:
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.
FWIW, Javascript, unlike PHP, does *not* have associative arrays.
When you do the above code, you are actually creating an object aa with 2
properties, peanuts and popcorn. Thus aa.length will be zero unless you
set it yourself.
So yes, for .. in is the only way to iterate over elements, exactly like
you would iterate an object's elements.
I'll let someone else write the sorting code for you. lol
Dec 20 '07 #2
I'll let someone else write the sorting code for you. lol
That's ok. I thought there might be some special construct to sort
it. Thanks.
Dec 20 '07 #3
AKS
On Dec 20, 8:47 am, mk83...@yahoo.com wrote:
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, ': '));

Dec 20 '07 #4
mk*****@yahoo.com wrote:
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..._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
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Dec 21 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Stefan Richter | last post by:
Hi, I want to create an associative Array with a PHP variable (article ID) as Key and another associative array as it's value. How do I instanciate it, how can I fill it? I want something...
27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
6
by: mark4asp | last post by:
Suppose I have the following code. It functions to randomly select a city based upon the probabilities given by the key differences in the associative array. . Eg. because the key difference...
4
by: Robert | last post by:
I am curious why some people feel that Javascript doesn't have associative arrays. I got these definitions of associative arrays via goggle: Arrays in which the indices may be numbers or...
8
by: Derek Basch | last post by:
Is there any way to associate name/value pairs during an array initialization? Like so: sType = "funFilter" filterTypeInfo = ; filterTypeInfo = new Array("type" : sType); I can do it using...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
7
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
11
by: Bosconian | last post by:
I'm trying to output the contents of an array of associative arrays in JavaScript. I'm looking for an equivalent of foreach in PHP. Example: var games = new Array(); var teams = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.