I am trying to identify duplicate object names in an Illustrator file using ExtendScript so I can group them. I can get an array of all PageItems on a layer, I just don’t know how to identify and store duplicates so I can perform the group operation.
This is a rather pathetic attempt at doing what I want:
var aLayer = app.activeDocument.activeLayer;
var pageItemsRef = aLayer.pageItems;
var duplicateNames = [];
var currentName;
for (var i = 0, len = pageItemsRef.length; i < len; i++) {
if(i==0){
currentName = pageItemsRef[0].name;
continue;
}
if(pageItemsRef[i].name == currentName){
var ref = {name:pageItemsRef[i].name, index:i}
duplicateNames.push(ref);
}
}
Continuing with this code will only give me consecutive duplicates. Sorry for the low quality example but I just don’t know where to start.
I just want to iterate pageItemsRef
and store the name + ID into separate arrays.
So array [0] will have 2+ duplicates stored as objects (Name, ID)
Array [1] might have 5+ duplicates stored as objects also stored as (Name,ID)
etc.
Any more efficient approaches are warmly welcomed as well. I am new to ExtendScript.
Any help greatly appreciated.
3
Answers
You can use reduce method to find duplicate,
Try This"
You can count the number of times that each targeted property value appears, then filter the array based on whether the count is greater than
1
:TS Playground
Compiled JS from the playground link above: