Re: Adding several key-value pairs at a time to Dictionary object

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Anthony Jones wrote:
"Randy Webb" <HikksNotAtHome@xxxxxxx> wrote in message
news:6aqdnS9sPOqICMfZnZ2dnUVZ_v6dnZ2d@xxxxxxxxxxxxxx
Anthony Jones said the following on 5/4/2006 5:43 PM:
"Randy Webb" <HikksNotAtHome@xxxxxxx> wrote in message
news:t8GdnXqCgqYc3sfZRVn-tA@xxxxxxxxxxxxxx
Anthony Jones said the following on 5/4/2006 9:44 AM:
"Randy Webb" <HikksNotAtHome@xxxxxxx> wrote in message
news:AM6dnTtRyM5dBMTZnZ2dnUVZ_uidnZ2d@xxxxxxxxxxxxxx
K said the following on 5/3/2006 3:01 PM:
Hi!

Here is a question I have: I am loading a large number of
dictionary entries into a dictionary ActiveX object in a JS
file. This means I repeat the statement

MyDictionary.Add("key n","value n");

thousands of times. Does anyone know whether its possible to
fill a dictionary by any other syntax?
Where is the data for the dictionary coming from? Where it is
coming from will give you some insight on the best way to create
the Dictionary object from it.

Or is javascript's associative arrays better for this purpose??
Javascript doesn't have an associative array.
var o = new Object()

o['MyKey1'] = 'Hello'
o['MyKey2'] = 'World'

Gets close enough for most uses as long as you don't also need to
access items by an ordinal index.
"Close enough", yes, but an Associative Array? No. In fact, the
above is not even an Array. Even converting it to an Array:


http://en.wikipedia.org/wiki/Associative_array

That one is dead wrong with regards to JS, and another example of
why I have a lack of trust of anything I read in a wiki.


Really?? What's wrong with it exactly?

http://www.comp.leeds.ac.uk/Perl/associative.html

PERL has an Associative Array, as does PHP, but that doesn't change
the fact the JS doesn't.


The purpose of posting these links is to provide a general idea of
what an Associative Array is.

http://www.nist.gov/dads/HTML/assocarray.html

That one is irrelevant to JS.

Again for the purpose of defining an associative array.


I could go on but I think the point is made.

If your point is that I am right and that Javascript doesn't have an
Associative Array,
then yes, your point is made. If you think your point
that you think JS has an Associative Array is made, then no, you are
incorrect. JS doesn't have an Associative Array.

I didn't actually say that it did. But you saw fit post a response
as if I did.

The code I posted showed that, very clearly. Here it is again:

var o = new Array()
o['MyKey1'] = 'Hello'
o['MyKey2'] = 'World'

If o were an Associative Array, with its corresponding length
property, then the length would be 2. It isn't, the above code gives
a length, correctly, of 0.

Agreed. You don't get to discover how many entries you have in the
array which is pretty fundemental to anything that wants to call
itself an array.


Now, does it act similar to an Associative Array? Sure. But that
doesn't make it an Associative Array, it makes it an Array/Object
that behaves, mildly, like an Associative Array.


Pretty darn close enough for many applications. In a lot of cases
you don't need or want to access the content by ordinal and how many
entries are present is also irrelevant. In these cases Object will
do fine.

This entire subject has been discussed, to death, in
comp.lang.javascript, you can search the archives and read about it.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup
weekly Javascript Best Practices -
http://www.JavascriptToolbox.com/bestpractices/

Blah, blah, blah, lolol

Assuming, of course, that K actually needs the associativity...


function AssocArray()
{
}

AssocArray.prototype.Add = function (valueA, valueB)
{
this [vValueA] = vValueB;
this [vValueB] = vValueA;
this [this.length] = vValueA; // If indexing is desired and no values
are numeric in nature.
this.count = this.length // If you prefer this property.
}

implementer: Hey, Javascript's got a very primitive associative array.
Woohoo!! :-)))

critic: Yeah, but it's not built in. z:-|

implementer: Oh, that makes it useless then, I guess. :-(((

critic: Well, no, that's not what I was saying.

implementer: Hey, Javascript's got a very primitive associative array.
Woohoo!! :-)))

critic: Philistine.

DotNuttah: LOLOL


.



Relevant Pages

  • Re: Adding several key-value pairs at a time to Dictionary object
    ... Javascript doesn't have an associative array. ... PERL has an Associative Array, as does PHP, but that doesn't change the ... Again for the purpose of defining an associative array. ... You don't get to discover how many entries you have in the array ...
    (microsoft.public.scripting.jscript)
  • Re: Retrieving array values by range of keys
    ... Suppose I have an associative array that looks like this: ... Let's assume that the array is reasonably large (hundreds of entries). ... (without doing a foreach/strncmp loop?) ... should give you all the keys that start with 'beta' ...
    (comp.lang.php)
  • Re: retrieve top n entries from an associative array
    ... After sorting an associative array by value, how can I retrieve top n ... entries from it? ... Zhidian Du ...
    (php.general)
  • Retrieving array values by range of keys
    ... Suppose I have an associative array that looks like this: ... Let's assume that the array is reasonably large (hundreds of entries). ... (without doing a foreach/strncmp loop?) ...
    (comp.lang.php)
  • retrieve top n entries from an associative array
    ... After sorting an associative array by value, how can I retrieve top n ... entries from it? ... Zhidian Du ...
    (php.general)