Skip to main content
Known Participant
December 6, 2024
Answered

How to add a header child to array

  • December 6, 2024
  • 3 replies
  • 678 views

I am trying to create a script that iterates thru the selected pathItems on the artboard and adds their name to an array but at the start of each group of pathItems that have the same name I would like a header child.

 

Artboard pathItems:

3 - pathItem.name = lemon

2 - pathItem.name = orange

3 - pathItem.name = peach

 

Header child:

pathObj.addChild("These are lemons:");

pathObj.addChild("These are oranges:");

pathObj.addChild("These are peaches:");

 

Resulting array:

[These are lemons:, lemon, lemon, lemon, These are oranges:, orange, orange, These are peaches:, peach, peach, peach]

 

Any help would be greatly appreciated.

 

This topic has been closed for replies.
Correct answer CarlosCanto

here's a sample

 

// map pathItems to json 12/5/24
// https://community.adobe.com/t5/illustrator-discussions/how-to-add-a-header-child-to-array/m-p/15022564#M429214

function main() {
    var idoc = app.activeDocument;
    
    var pItems = idoc.pathItems;
    
    var fruits = {};
    
    var pname;
    
    for (var b=0; b<pItems.length; b++) {
        pname = pItems[b].name;
        
        // if key does not exist, create one and initialize it with an empty array
        if (!fruits[pname]) {
            fruits[pname] = [];
        }
    
        fruits[pname].push(pname);
        
    }

    //alert(fruits.toSource());
    //alert(fruits["orange"].toSource());
    
    // then to consume the data
    alert("There are: " + fruits["orange"].length + " Oranges");
   
}

main();

3 replies

CarlosCanto
Community Expert
Community Expert
December 6, 2024

now if it is imperative to have an Array I'll give it another try.

m1b
Community Expert
Community Expert
December 6, 2024

@CarlosCanto You can use named members of arrays in ES3. If you just change to

var fruits = [];

I think .your script will work the same. (However like you I would use an object here.)

 

But I'm actually wondering if OP had something entirely different in mind but I'm not sure what. Maybe a visual object to label the oranges etc?

- Mark

 

CarlosCanto
Community Expert
Community Expert
December 6, 2024

Hi Mark, for real? I did not know that 😕😕

 

I did what you mentioned and the script works the same, but fruits.toSource() came up empty, but the rest worked as expected

CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
December 6, 2024

here's a sample

 

// map pathItems to json 12/5/24
// https://community.adobe.com/t5/illustrator-discussions/how-to-add-a-header-child-to-array/m-p/15022564#M429214

function main() {
    var idoc = app.activeDocument;
    
    var pItems = idoc.pathItems;
    
    var fruits = {};
    
    var pname;
    
    for (var b=0; b<pItems.length; b++) {
        pname = pItems[b].name;
        
        // if key does not exist, create one and initialize it with an empty array
        if (!fruits[pname]) {
            fruits[pname] = [];
        }
    
        fruits[pname].push(pname);
        
    }

    //alert(fruits.toSource());
    //alert(fruits["orange"].toSource());
    
    // then to consume the data
    alert("There are: " + fruits["orange"].length + " Oranges");
   
}

main();
CarlosCanto
Community Expert
Community Expert
December 6, 2024

it might be possible to make an array but the way you described it, an Object might be easier to consume after it has been made instead of an Array

 

fruits = {

  lemon: [lemon, lemon, lemon],

  orange: [orange, orange],

  peach: [peach, peach, peach]

}