如何使用ExtendScript从数组中提取重复项?

huangapple go评论73阅读模式
英文:

How can I extract duplicates from an array using ExtendScript?

问题

我正在尝试使用ExtendScript在Illustrator文件中识别重复的对象名称,以便我可以对它们进行分组。我可以获取一个图层上所有PageItems的数组,我只是不知道如何识别和存储重复项,以便执行分组操作。

这是一个实现我想要的功能的相当可悲的尝试:

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

继续使用这段代码只会给我连续的重复项。抱歉,这个示例质量较低,但我不知道从哪里开始。

我只想迭代pageItemsRef并将名称+ID存储到单独的数组中。

因此,数组[0]将具有存储为对象(名称、ID)的2个或更多重复项。
数组[1]可能会有存储为对象的5个或更多重复项,也存储为(名称、ID)。
等等。

任何更有效的方法也受到热烈欢迎。我是ExtendScript的新手。

非常感谢任何帮助。

英文:

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 &lt; 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.

答案1

得分: 1

我建议使用中间对象 obj 来实现这个经典解决方案。由于一个对象不能具有相同名称的键,它可以用作将具有相同属性(在这种情况下是名称)的项聚合到一个值中的“过滤器”:

var layer = app.activeDocument.activeLayer;
var items = layer.pageItems;

// 中间对象 {name1:[items], name2:[items], ...}
var obj = {};
for (var i = 0; i < items.length; i++) {
    var item = items[i];
    var info = { name: item.name, id: item.uuid };  // &lt;-- ID 是 .uuid 吗?
    if (item.name in obj) obj[item.name].push(info);
    else obj[item.name] = [info];
}

// 从中间对象的键创建二维数组
// [ [{name1,id1},{name1,id2}], [{name2,id3},{name2,id4},...], ... ]
var duplicates = [];
for (var key in obj) duplicates.push(obj[key]); // &lt;-- 这是关键

// 也许你甚至不需要数组
// 也许你可以直接使用 obj

唯一让我困惑的是:你所说的“ID”是什么意思?在我的代码中,我使用了页面项的本机 uuid 属性。或者你只是指普通的计数器(索引)?

英文:

I'd propose this classic solution via the intermediate object obj. Since an object can't have keys with the same name it can be used as a 'filter' to gather into a value the items with the same property (a name in this case):

var layer = app.activeDocument.activeLayer;
var items = layer.pageItems;

// the intermediate object {name1:[items], name2:[items], ...}
var obj = {};
for (var i=0; i&lt;items.length; i++) {
    var item = items[i];
    var info = {name:item.name, id:item.uuid};  // &lt;-- ID is .uuid ???
    if (item.name in obj) obj[item.name].push(info);
    else obj[item.name] = [info];
}

// make the 2d-array from the keys of the intermediate object
// [ [{name1,id1},{name1,id2}], [{name2,id3},{name2,id4},...], ... ]
var duplicates = [];
for (var key in obj) duplicates.push(obj[key]); // &lt;-- here we go

// probably you don&#39;t even need the array
// perhaps you can use the obj instead

The only thing I don't get: what do you mean by 'ID'? In my code I'm using the native uuid property of a page item. Or do you mean just the plain counter (index)?

答案2

得分: 0

var aLayer = app.activeDocument.activeLayer;
var pageItemsRef = aLayer.pageItems;
var duplicates = {};

for (var i = 0, len = pageItemsRef.length; i < len; i++) {
    var currentItemName = pageItemsRef[i].name;
    if (duplicates[currentItemName] == null) {
        duplicates[currentItemName] = [];
    }
    duplicates[currentItemName].push({name: currentItemName, index: i});
}
英文:

Try This"

var aLayer = app.activeDocument.activeLayer;
var pageItemsRef = aLayer.pageItems;
var duplicates = {};

for (var i = 0, len = pageItemsRef.length; i &lt; len; i++) {
    var currentItemName = pageItemsRef[i].name;
    if (duplicates[currentItemName] == null) {
        duplicates[currentItemName] = [];
    }
    duplicates[currentItemName].push({name: currentItemName, index: i});
}

huangapple
  • 本文由 发表于 2023年6月26日 11:45:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553400.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定