Skip to main content
Known Participant
Dec 6, 2024 2:26 AM UTC
Answered

How to add a header child to array

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 is closed to new replies. Start a new post to keep the conversation going.
CarlosCanto
CarlosCantoCorrect answer
Community Expert
Dec 6, 2024 5:06 AM UTC

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();

6 replies

CarlosCanto
Community Expert
Community Expert
Dec 6, 2024 5:11 AM UTC

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

Translate
m1b
Community Expert
Community Expert
Dec 6, 2024 5:49 AM UTC

@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

 

Translate
CarlosCanto
Community Expert
Community Expert
Dec 6, 2024 5:58 AM UTC

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

Translate
CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
Dec 6, 2024 5:06 AM UTC

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();
Translate
CarlosCanto
Community Expert
Community Expert
Dec 6, 2024 4:36 AM UTC

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]

}

Translate