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

getting the .length of a global array

Explorer ,
Oct 20, 2020 Oct 20, 2020

I'm trying to retrieve the length of a global array (one i constructed in Docunebnt JavaScriots).

 

event.value = gTable.length;

 

It returns a value of 0. I've tried it with a loval array, and it works just fine. I'm able to access objects in the array, but for some reason I can't get it's length...

TOPICS
Create PDFs , PDF forms
2.1K
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
1 ACCEPTED SOLUTION
Explorer ,
Oct 21, 2020 Oct 21, 2020

I get 0.

There are 4 entries in the array. I'm using the same method for creating my array as I did before, which JS handles like a standard object, or so I've read. It's an associative array with names indexes. Might this be the issue?

 

var gTable = new Array;
gTable[' '] = ' -- -- -- ';
gTable['Item 1'] = 'Item 1--1H--25--3';
gTable['Item 2'] = 'Item 2--2H--17--4';
gTable['Item 3'] = 'Item 3--1H--9--7';

View solution in original post

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
Community Expert ,
Oct 20, 2020 Oct 20, 2020

Try this:

Execute

gTable.length;

in the Javascript console.

What does you get?

 

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
Explorer ,
Oct 21, 2020 Oct 21, 2020

I get 0.

There are 4 entries in the array. I'm using the same method for creating my array as I did before, which JS handles like a standard object, or so I've read. It's an associative array with names indexes. Might this be the issue?

 

var gTable = new Array;
gTable[' '] = ' -- -- -- ';
gTable['Item 1'] = 'Item 1--1H--25--3';
gTable['Item 2'] = 'Item 2--2H--17--4';
gTable['Item 3'] = 'Item 3--1H--9--7';

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
Community Expert ,
Oct 21, 2020 Oct 21, 2020

You can get the number of entries with:

Object.keys(gTable).length;

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
Community Expert ,
Oct 21, 2020 Oct 21, 2020

That won't work in earlier versions of Acrobat or Reader, though.

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
Explorer ,
Oct 21, 2020 Oct 21, 2020

That worked. Not teribly concerne with older versions. I will have to add that check, I suppose, just in case...

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
Community Expert ,
Oct 21, 2020 Oct 21, 2020

Or just use the code I provided, which works in all circumstances...

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
Community Expert ,
Oct 21, 2020 Oct 21, 2020

How did you declare this array? How are you accessing values within it?

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
Explorer ,
Oct 21, 2020 Oct 21, 2020

I've posted above how I construct the array.

I'm using 'gTable[event.changeEx].split('--')[0]' to access the valuse in the array.

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
Community Expert ,
Oct 21, 2020 Oct 21, 2020

OK, that's what I suspected... This is not an array. It's a literal object. Arrays don't have names for the items in them, and literal objects don't have a "length" property. If you want count how many items are in your object you can do it like this:

var counter = 0; for (var i in gTable) counter++; app.alert("Number of items in gTable: " + counter);

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
Explorer ,
Oct 21, 2020 Oct 21, 2020

This is working, so I'm trying to confirm the entries. Not sure why it's not working...

 

// get the number of entries in the table
var itemNum = 0;
for (var i in gTable) itemNum++;

// list the items in the table
var i = 0;
while (i <= itemNum) {
console.println(gTable[i].split("--")[0])
i ++;
}

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
Community Expert ,
Oct 21, 2020 Oct 21, 2020

Because the items in the literal object have to be accessed by name (or an iterator), not a number. This is NOT AN ARRAY!

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
Explorer ,
Oct 21, 2020 Oct 21, 2020
LATEST

Crap, right... Can't have it both ways...

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