Closed Thread Icon

Topic awaiting preservation: JavaScript Associative Array? (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8878" title="Pages that link to Topic awaiting preservation: JavaScript Associative Array? (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: JavaScript Associative Array? <span class="small">(Page 1 of 1)</span>\

 
Dick Ulrich
Bipolar (III) Inmate

From: Dublin, TX, USA
Insane since: Sep 2003

posted posted 09-30-2003 04:54

Hi,

Just ran into an interesting situation and wondered if I am hallucinating.
What a better place to find out than an Asylum ?

I was using strings as indexes to store some information.
I used the string "length" as one of the indices.
Hmmmm, that stopped my browser in its tracks.
Has anyone seen it documented that you can't use that freely as an associative string index?
Of course it returns length of the array in an alert! Which is 0

Just curious ...

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 09-30-2003 05:05

do you have a sample?

I threw this together and it works fine

<script>
h = new Array();
for(i=0;i<30;i++) {
h[i] = "ASDFS"+i;
}
str = "thisbe";
alert(h[str.length]);
</script>



.:[ Never resist a perfect moment ]:.

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 09-30-2003 05:42

I think Dick Ulrich rather meant something like:

code:
myArray = new Array()
myArray[ "anAssociativeKey" ] = "something"
myArray[ "anotherAssociativeKey" ] = "something else"
// myArray[ "length" ] = "something different" // uncomment this line to make the whole thing crash
blah = ""
for( i in myArray )
blah += i +" => "+ myArray[ i ] +"\n"
alert( blah )

Indeed length is a property of the Array object and cannot be overwritten with something that is not numerical. In Javascript, Array's and Object's properties / values can be accessed either via :

objectName[ propertyName ]
or
objectName.propertyName

[edit]Note that only the first syntax works[/edit] if propertyName is numerical.
See the following pages : Objects and Properties , Array Object , length. It is implicitly stated when it's mentionned that the length is an integer.

Hope that helps.

Mathieu "POÏ" HENRI
[edit]fixing a misguiding remark which may have lead to parsing errors. Thank you Slime[/edit]

[This message has been edited by poi (edited 09-30-2003).]

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 09-30-2003 07:30

One should note that, internally, arrays and objects are the *same thing*. So, these two things are equivalent:

myobj["property"] = 3;
myobj.property = 3;

Similarly, these two things are also equivalent:

myobj[5] = 3;
myobj.5 = 3;

However, the latter just happens to be invalid syntax because of parsing limitations. But the point is, the same thing is happening internally. So when you say myarray["length"], you're really saying myarray.length, which has a predefined value.

Dick Ulrich
Bipolar (III) Inmate

From: Dublin, TX, USA
Insane since: Sep 2003

posted posted 09-30-2003 07:40

Mathieu "POÏ" HENRI most eloquently laid it out, unlike all of the documentation on the web about Javascript Associative Arrays.

Sigh ... and just why would I want to put all the DOM attributes, etc. into an associative array?
Sigh ... the authors of the DOM spec should have left 'length' out !!

Good thing Associative Arrays aren't spatial in nature .. (length, width, depth,shape,rank) (chuckle)

Thanks for listening to me moan ... what an oxymoron to have 562 elements in an associative array
and get 0 back for its["length'] !! What were they thinking ??? As my wife said, Length is very important and I guess this proves that.

Smile



« BackwardsOnwards »

Show Forum Drop Down Menu