Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add variable to array in a loop

using extend script to push a variable into an array it's basically javascript. any idea what I am doing wrong?

if ( app.documents.length > 0 ) {

    for ( i = 0; i< app.activeDocument.textFrames.length; i++) {
         var allSizes = []; //set up empty array

        textArtRange = app.activeDocument.textFrames[i].textRange;
        var fontName =  textFonts.getByName("Nobile");
        alert (fontName);
        textArtRange.characterAttributes.textFont = fontName;
        var fontSizes = textArtRange.characterAttributes.size;

        allSizes.push(fontSizes)
        alert (fontSizes);

    }
        alert (allSizes);
}

the alerts for allSizes only return single values, not the array.

like image 305
Lukasz Avatar asked Oct 03 '11 17:10

Lukasz


People also ask

How do you add variables to an array?

When you want to add an element to the end of your array, use push(). If you need to add an element to the beginning of your array, try unshift(). And you can add arrays together using concat().


2 Answers

Move the definition of allSizes = [] outside the loop.

Currently, you're "resetting" the value of allSizes at each loop.

like image 94
Rob W Avatar answered Sep 26 '22 09:09

Rob W


You're setting up the empty array inside of the for loop. It's resetting it each time. Move it above the for loop:

var allSizes = []; //set up empty array
for ( i = 0; i< app.activeDocument.textFrames.length; i++) {
     .....
like image 42
Chris Eberle Avatar answered Sep 26 '22 09:09

Chris Eberle