Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

JS CS5.5: Problem in Associative array (May be a bug)

Enthusiast ,
Jun 16, 2011 Jun 16, 2011

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?

TOPICS
Scripting
1.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 16, 2011 Jun 16, 2011

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jun 17, 2011 Jun 17, 2011

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 17, 2011 Jun 17, 2011

Property names can not have spaces in them.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Jun 17, 2011 Jun 17, 2011

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]);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 17, 2011 Jun 17, 2011

Mark, what is your example intended to demonstrate?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 17, 2011 Jun 17, 2011

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jun 17, 2011 Jun 17, 2011

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 20, 2011 Jun 20, 2011

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 20, 2011 Jun 20, 2011
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines