Skip to main content
Inspiring
March 20, 2013
Question

Help, pls, fill an array... [js]

  • March 20, 2013
  • 2 replies
  • 1486 views

I chek all graphics (num in order, type, page) in doc and try to get into array. How to whrite it in array?

i'm lost in this:

var mColl = [[],[3]]; //declare 2d-array

for(i = 0; i<aD.allGraphics.length; i++){

var myGraphic = aD.allGraphics;

        if (myGraphic.parentPage !=null) { //if graph not in desktop

     $.writeln ( i );

        $.writeln (myGraphic.imageTypeName);

        $.writeln ("on page " + myGraphic.parent.parentPage.name + "\n");

// and here i'd like to collect all this data in array       

        mColl[i, 0] = mColl.push(i);

        mColl[i, 1] = mColl.push(myGraphic.imageTypeName);

        mColl[i, 2] = mColl.push(myGraphic.parent.parentPage.name);

        }

}

alert (mColl); // so far i have in this big mess

This topic has been closed for replies.

2 replies

Marc Autret
Legend
March 22, 2013

Or:

var source = app.activeDocument,

    graphics = source.allGraphics,

    n = graphics.length,

    a = [], // your results

    z = 0,

    gx, pp;

for( i=0 ; i < n ; ++i )

    {

    (pp=(gx=graphics).parentPage) &&

        a[z++] = [i, gx.imageTypeName, pp.name];

    }

alert( a.join('\r') );

// Each resulting item is then in the form:

// [ <index-in-graphics>, <image-type-name>, <parent-page-name> ]

@+

Marc

Trevor:
Legend
March 22, 2013

Hi Marc,

I was wondering why you didn't use push instead of z++

(pp=(gx=graphics).parentPage) &&
        a.push([i, gx.imageTypeName, pp.name]);

Was is the time or more likely  am I missing something? 

Regards

Trevor

Marc Autret
Legend
March 22, 2013

Hi Trevor,

I got into the habit of avoiding Array.push(), pop() etc. when possible, as these methods seem to be slow in ExtendScript. See for example: http://www.indiscripts.com/post/2011/06/comparing-the-performance-of-extendscript-snippets

@+

Marc

SureshRaji
Inspiring
March 20, 2013

Hi,

I am new to javascript. I have a doubt.

What will happen if we use,

1.  mColl[i, 0] = i;

     mColl[i, 1] = myGraphic.imageTypeName;

     mColl[i, 2] = "on page " + myGraphic.parent.parentPage.name + "\n";

     alert(mColl);

instead of,

2.  mColl[i, 0] = mColl.push(i);

     mColl[i, 1] = mColl.push(myGraphic.imageTypeName);

     mColl[i, 2] = mColl.push("on page " + myGraphic.parent.parentPage.name + "\n");

I tried the first. But after exiting the loop, it doesn't give all the values.

Regards

Sureshkumar

Jump_Over
Legend
March 20, 2013

Hi,

Declare mCol just like empty one [ [],[] ]

Fill it with data this way:

mCol.push(i);

mColl.push(myGraphic.imageTypeName);

Refer to your 2-D array this way:

     mColl[0];

     mColl[1];

and so on...

rgds

Jarek

BeliakovAuthor
Inspiring
March 21, 2013

worked this way

var mColl = [];

for (i = 0; i < aD.allGraphics.length-1; i++){

  var myGraphic = aD.allGraphics;

  if (myGraphic.parentPage !=null){

      mColl.push(["\n" + i, myGraphic.imageTypeName, myGraphic.parent.parentPage.name]);

  }

}

alert(mCall);