JS CS5.5: Problem in Associative array (May be a bug)
Copy link to clipboard
Copied
Hi,
var myArray = new Array();
myArray ['8 test'] = "Value 1";//I can see this value in data browser
myArray ['85 test'] = "Value 2";//But I can not see this
myArray ['1 test'] = "Value 2";
var len = 0;
for (x in myArray){
len++;
}
alert(len);//But returns correct value
In data browser value of myArray['85 test'] is not shown. But i can see rest of the values.
Did any one came across this problem?
Copy link to clipboard
Copied
JavaScript Arrays are not associative arrays.
You want an Object!
(Also, for an Array, most prefer the notation myArray = [];).
So use var myArray = {};
(or you could say new Object() but I wouldn't).
You should probably also not name it myArray for the same reasons.
Copy link to clipboard
Copied
Hi John,
As you said, I have tried with this code. Still same result showing the ESTK's data browser.
var styles = [];//Tried with {} also
styles["8 yes"] = "yes";
styles["85 No"] = "No";//Problem Here only
styles["t85 yes"] = "yes";
styles["9 yes"] = "yes";
alert("Pause Here");
Run the above code step by step and check "styles []" after executing each line in the data browser.
is this is a normal behaviour?
------------
Green4ever
Copy link to clipboard
Copied
Property names can not have spaces in them.
Copy link to clipboard
Copied
Nor would they be arrays…
var styles = {}; // Object styles.yes8 = 'yes'; styles.yes85 = 'No'; styles.t85 = 'yes'; styles.yes9 = ['yes','No','Yes']; alert(styles.yes85); alert(styles.yes9[0]);
Copy link to clipboard
Copied
Mark, what is your example intended to demonstrate?
Copy link to clipboard
Copied
Sorry, I suppose I wasn't paying as close attention as I should have.
The reason to use an Object and not an Array is that Arrays are Objects with special features (like a .length property), and when you treat them like Objects, you get very confusing behavior when you access those special features. And if you don't ever use those special features, it all works, but then you there was no point to using the Array. For instance:
>> a = [ 1, 2, 3]
1,2,3
>> a[0]
1
>> a.length
3
>> a.pizza=4
4
>> a.length
3
>> a[4]
undefined
>> a['pizza']
4
There is nothing wrong with spaces in property names. It certainly sounds like it confuses the ESTK's Data Browser, so I guess that's a bug, yeah.
Copy link to clipboard
Copied
Hi Harbs,
Property names can not have spaces in them.
I have tried this without space also. Same result came.
Hi John,
Thanks for paying close attention. 🙂
It certainly sounds like it confuses the ESTK's Data Browser, so I guess that's a bug, yeah.
The value is not shown in the data browser, but if you alert it, it will show the correct result. Anyway my code works... 😉
------
Green4ever
Copy link to clipboard
Copied
Same bug in ESTK 3 / CS4.
The data browser definitely has a problem in displaying object properties whose name starts with a (recurring) digit. The issue is not related to property names that could contain a space character —which is not forbidden! Curiously, it seems that the bug only occurs with digits greater than 1.
Compare:
var obj = {
"0a": null,
"00b": null,
"1a": null,
"11b": null
};
// The data browser properly displays all obj properties
and:
var obj = {
"2a": null,
"22b": null,
"5a": null,
"55b": null
};// The data browser only displays obj['5a'] and obj['5b']!
Note that this is not an internal ExtendScript bug. In all cases the object is properly set, the for...in loop works fine and obj.__count__ returns the correct number of enumerable properties.
Conclusion: ESTK sucks!
@+
Marc
Copy link to clipboard
Copied
Sorry about that with the spaces.
Somehow I never realized you could do that (I guess I mixed up dot notation and bracket notation...)
It's very useful to know I was wrong! It looks like there's no limitation to what characters can be used as a key.
Thanks guys!
Harbs

