Copy link to clipboard
Copied
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...
Copy link to clipboard
Copied
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';
Copy link to clipboard
Copied
Try this:
Execute
gTable.length;
in the Javascript console.
What does you get?
Copy link to clipboard
Copied
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';
Copy link to clipboard
Copied
You can get the number of entries with:
Object.keys(gTable).length;
Copy link to clipboard
Copied
That won't work in earlier versions of Acrobat or Reader, though.
Copy link to clipboard
Copied
That worked. Not teribly concerne with older versions. I will have to add that check, I suppose, just in case...
Copy link to clipboard
Copied
Or just use the code I provided, which works in all circumstances...
Copy link to clipboard
Copied
How did you declare this array? How are you accessing values within it?
Copy link to clipboard
Copied
I've posted above how I construct the array.
I'm using 'gTable[event.changeEx].split('--')[0]' to access the valuse in the array.
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
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 ++;
}
Copy link to clipboard
Copied
Because the items in the literal object have to be accessed by name (or an iterator), not a number. This is NOT AN ARRAY!
Copy link to clipboard
Copied
Crap, right... Can't have it both ways...
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more