javascript get array key name

  • Tdotwire
  • Proficient
  • Proficient
  • User avatar
  • Joined: Jul 18, 2004
  • Posts: 486
  • Loc: Toronto
  • Status: Offline

Post April 30th, 2005, 11:03 pm

for some reason when I make an associative array in javascript I cannot get the length of it.

Code: [ Download ] [ Select ]
var ar = new Array();
ar['nice'] = "hello";
alert(ar.length);

//keeps on giving zero
  1. var ar = new Array();
  2. ar['nice'] = "hello";
  3. alert(ar.length);
  4. //keeps on giving zero


But when I have a regualr index array 0,1,3... it gives me the length,

what am I doing wrong
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post April 30th, 2005, 11:03 pm

  • Tdotwire
  • Proficient
  • Proficient
  • User avatar
  • Joined: Jul 18, 2004
  • Posts: 486
  • Loc: Toronto
  • Status: Offline

Post April 30th, 2005, 11:05 pm

how do would I get the KEY name of the array...

var x = new Array();

x['key'] = "value";


now how would I get the name of that array key...?

Post May 1st, 2005, 12:02 am

If I remember correctly, associative arrays in javascript are treated as objects and not as regular arrays. Because of this, they do not have a length property because the array "elements" are now properties of an object. To get a count of the properties, you have to loop over the object and count them like this:
Code: [ Download ] [ Select ]
var ar_ct = 0;
var ar = new Array();
ar['nice'] = "hello";
for (i in ar)
{
    ar_ct++;
}
alert (ar_ct);
  1. var ar_ct = 0;
  2. var ar = new Array();
  3. ar['nice'] = "hello";
  4. for (i in ar)
  5. {
  6.     ar_ct++;
  7. }
  8. alert (ar_ct);

Post May 1st, 2005, 12:07 am

here we go again with the looping :)

To get the key name, you either have to know it, or loop over the object:
Code: [ Download ] [ Select ]
var x = new Array();
x['key'] = "value";
for (i in x)
{
    if (i == 'key')
    {
        alert ("we got "+i);
    }
}
  1. var x = new Array();
  2. x['key'] = "value";
  3. for (i in x)
  4. {
  5.     if (i == 'key')
  6.     {
  7.         alert ("we got "+i);
  8.     }
  9. }
  • joebert
  • Weathered
  • Genius
  • User avatar
  • Joined: Feb 10, 2004
  • Posts: 11872
  • Loc: Clearwater, FL
  • Status: Offline

Post May 1st, 2005, 5:28 pm

// Merged
This is kinda interesting,

Code: [ Download ] [ Select ]
x = new Array('a','b','c');

alert(typeof x); // object ?!?

alert(x.toString()); // a,b,c

x['key'] = 'd';

alert(x.toString()); // a,b,c

x[3] = 'e';

alert(x.toString()); // a,b,c,e

_ = typeof x + ",";
for(i in x){
    _ += i + ",";
}
alert(_); // object,0,1,2,key,3,
  1. x = new Array('a','b','c');
  2. alert(typeof x); // object ?!?
  3. alert(x.toString()); // a,b,c
  4. x['key'] = 'd';
  5. alert(x.toString()); // a,b,c
  6. x[3] = 'e';
  7. alert(x.toString()); // a,b,c,e
  8. _ = typeof x + ",";
  9. for(i in x){
  10.     _ += i + ",";
  11. }
  12. alert(_); // object,0,1,2,key,3,
Why yes, yes I am.
  • RichB
  • Guru
  • Guru
  • User avatar
  • Joined: May 17, 2003
  • Posts: 1121
  • Loc: Boston
  • Status: Offline

Post May 1st, 2005, 7:35 pm

curiouser and curiouser:

Code: [ Download ] [ Select ]
x = new Array();

x['key'] = 'd';

alert("toString = " + x.toString()); // toString =
alert("x.length = " + x.length); // x.length = 0

x[0] = 'e';

alert("toString = " + x.toString()); // toString = e
alert("x.length = " + x.length); // x.length = 1

_ = typeof x + ",";
for(i in x){
  _ += i + ",";
}
alert(_); // object,key,0,
alert("x.key = " + x.key); // x.key = d
  1. x = new Array();
  2. x['key'] = 'd';
  3. alert("toString = " + x.toString()); // toString =
  4. alert("x.length = " + x.length); // x.length = 0
  5. x[0] = 'e';
  6. alert("toString = " + x.toString()); // toString = e
  7. alert("x.length = " + x.length); // x.length = 1
  8. _ = typeof x + ",";
  9. for(i in x){
  10.   _ += i + ",";
  11. }
  12. alert(_); // object,key,0,
  13. alert("x.key = " + x.key); // x.key = d


It looks like when you add an associative element that it makes it a property of the Object like Sam said, rather than including it in the Array construct itself - if that makes sense. It doesn't show a length value until a numerical index is used and the value with the non-numeric index doesn't show up in the toString output as in Joebert's example. Yet all the array elements and Object properties can still be accessed through the for...in loop (or are all the array elements technically properties and only those with a numerical index count as part of the Array construct?). I'm a little confuzzled to tell the truth.

:?
Free Programming Resources

Post May 2nd, 2005, 12:06 am

Actually, all arrays are treated as objects. It appears, however, that they refrence named elements as properties and numerically indexed elements as simply data associated with the object.
Consider the following even more curioser code:
Code: [ Download ] [ Select ]
a = new Array();
a[0] = 'poop';
a[1] = 'crap';

alert(a.length); // 2
alert(typeof(a)); // object

b = new Array();
b[0] = 'poop';
b[1] = 'crap';
b['what'] = 'dookie';

alert(b.length); // 2
alert(typeof(b)); // object
  1. a = new Array();
  2. a[0] = 'poop';
  3. a[1] = 'crap';
  4. alert(a.length); // 2
  5. alert(typeof(a)); // object
  6. b = new Array();
  7. b[0] = 'poop';
  8. b[1] = 'crap';
  9. b['what'] = 'dookie';
  10. alert(b.length); // 2
  11. alert(typeof(b)); // object


They both report themselves as objects, and both with a length of 2
  • RichB
  • Guru
  • Guru
  • User avatar
  • Joined: May 17, 2003
  • Posts: 1121
  • Loc: Boston
  • Status: Offline

Post May 2nd, 2005, 12:29 am

Yes, I understand that arrays are objects, which makes sense after all (my use of the term "construct" was not a good choice). What struck me as odd is the behavior.

It might accidentally lead to something like this:

Code: [ Download ] [ Select ]
x = new Array();
x[0] = "zero";
x[1] = "one";
alert(x.length); // 2
x['length'] = 99;
alert(x.length); // 99
  1. x = new Array();
  2. x[0] = "zero";
  3. x[1] = "one";
  4. alert(x.length); // 2
  5. x['length'] = 99;
  6. alert(x.length); // 99
Free Programming Resources
  • joebert
  • Weathered
  • Genius
  • User avatar
  • Joined: Feb 10, 2004
  • Posts: 11872
  • Loc: Clearwater, FL
  • Status: Offline

Post May 2nd, 2005, 2:30 am

Conclusion I've drawn, there is no such thing as an associative array in Javascript.

There is only Objects with properties, the Objects properties can consist of ordered lists, the ordered lists can not contain named elements but they can contain properties of the ordered lists.

Obtaining the count for each unique word in a phrase is a pain in the a.. with reserved words & private properties.

Javascript had to be invented by someone with many "keep out" signs in their yard. :lol:
Why yes, yes I am.
  • RichB
  • Guru
  • Guru
  • User avatar
  • Joined: May 17, 2003
  • Posts: 1121
  • Loc: Boston
  • Status: Offline

Post May 2nd, 2005, 2:25 pm

I think that's pretty much the gist of it. I was looking through the Core JavaScript Guide and came across some things that would seem to confirm it:

Quote:
An array is an ordered set of values associated with a single variable name. Properties and arrays in JavaScript are intimately related; in fact, they are different interfaces to the same data structure.


One of the things that had me confused was the fact that js allows the HTML objects (i.e. images, forms, etc) to be referred to by either index or name, but it turns out that that is an exception:

Quote:
In JavaScript 1.0, you can refer to an object's properties by their property name or by their ordinal index. In JavaScript 1.1 or later, however, if you initially define a property by its name, you must always refer to it by its name, and if you initially define a property by an index, you must always refer to it by its index.

The exception to this rule is objects reflected from HTML, such as the forms array. You can always refer to objects in these arrays by either their ordinal number (based on where they appear in the document) or their name (if defined).


The guide did specifically refer to "associative arrays" once, but it was in the Ojbects and Properties secion and not under the Array Object section. As far as I can tell, all of the Array methods and the length property only apply to properties defined with an ordinal index. I'm thinking that I might use the Object constructor for "associative arrays" to avoid the possibility of confusion like overwriting the length property or calling something like join() where it won't work as expected.
Free Programming Resources

Post May 2nd, 2005, 6:12 pm

Richb, I don't think your length example is really a bug, it's essentially the same as setting the size of the array using
Code: [ Download ] [ Select ]
x = new Array(99);

with the exception that setting x['length'] can be used to truncate an array after the elements have been declared.
    If you set the length to 2 using either method, then add 3 numerically indexed elements, x.length will report 3.
    If you set the length to 50 using either method then add 3 numerically indexed elements, x.length will report 50.
    If you create the array with no declared size and add 3 numerically indexed elements, then set x['length'] to 2, x.length will report 2, a for in loop will only process the first 2 elements, and x[2] (the third element) will be undefined.

So the ability to set x.length does have a purpose, even if it is an obscure, somewhat confusing and rarely (if ever) used purpose.
It's nice to know it's there :P
  • RichB
  • Guru
  • Guru
  • User avatar
  • Joined: May 17, 2003
  • Posts: 1121
  • Loc: Boston
  • Status: Offline

Post May 2nd, 2005, 6:48 pm

Oh, I never said that it was a bug, merely that it had the potential to create bugs if you're not careful. Consider that if you create the array using the Array constructor and then use any key values like 'join', 'reverse' etc, then those methods are no longer available. I admit it's an unlikely scenario, but I'm going to be careful about it anyway.
Free Programming Resources

Post May 2nd, 2005, 7:54 pm

I agree, it does have potential for creating bugs, and I haven't really thought about it until now. But since associative arrays are objects, and you need the ability to create your own methods and properties for "regular" objects, I'd have to say that it is intended behavior, but definitely not well documented. I'll have to be careful about it as well.

Post Information

  • Total Posts in this topic: 13 posts
  • Users browsing this forum: No registered users and 121 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© Unmelted Enterprises 1998-2009. Driven by phpBB © 2001-2009 phpBB Group.