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

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

How can I extract duplicates from an array using ExtendScript?

问题

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

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

  1. var aLayer = app.activeDocument.activeLayer;
  2. var pageItemsRef = aLayer.pageItems;
  3. var duplicateNames = [];
  4. var currentName;
  5. for (var i = 0, len = pageItemsRef.length; i < len; i++) {
  6. if(i==0){
  7. currentName = pageItemsRef[0].name;
  8. continue;
  9. }
  10. if(pageItemsRef[i].name == currentName){
  11. var ref = {name:pageItemsRef[i].name, index:i}
  12. duplicateNames.push(ref);
  13. }
  14. }

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

我只想迭代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:

  1. var aLayer = app.activeDocument.activeLayer;
  2. var pageItemsRef = aLayer.pageItems;
  3. var duplicateNames = [];
  4. var currentName;
  5. for (var i = 0, len = pageItemsRef.length; i &lt; len; i++) {
  6. if(i==0){
  7. currentName = pageItemsRef[0].name;
  8. continue;
  9. }
  10. if(pageItemsRef[i].name == currentName){
  11. var ref = {name:pageItemsRef[i].name, index:i}
  12. duplicateNames.push(ref);
  13. }
  14. }

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

  1. var layer = app.activeDocument.activeLayer;
  2. var items = layer.pageItems;
  3. // 中间对象 {name1:[items], name2:[items], ...}
  4. var obj = {};
  5. for (var i = 0; i < items.length; i++) {
  6. var item = items[i];
  7. var info = { name: item.name, id: item.uuid }; // &lt;-- ID 是 .uuid 吗?
  8. if (item.name in obj) obj[item.name].push(info);
  9. else obj[item.name] = [info];
  10. }
  11. // 从中间对象的键创建二维数组
  12. // [ [{name1,id1},{name1,id2}], [{name2,id3},{name2,id4},...], ... ]
  13. var duplicates = [];
  14. for (var key in obj) duplicates.push(obj[key]); // &lt;-- 这是关键
  15. // 也许你甚至不需要数组
  16. // 也许你可以直接使用 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):

  1. var layer = app.activeDocument.activeLayer;
  2. var items = layer.pageItems;
  3. // the intermediate object {name1:[items], name2:[items], ...}
  4. var obj = {};
  5. for (var i=0; i&lt;items.length; i++) {
  6. var item = items[i];
  7. var info = {name:item.name, id:item.uuid}; // &lt;-- ID is .uuid ???
  8. if (item.name in obj) obj[item.name].push(info);
  9. else obj[item.name] = [info];
  10. }
  11. // make the 2d-array from the keys of the intermediate object
  12. // [ [{name1,id1},{name1,id2}], [{name2,id3},{name2,id4},...], ... ]
  13. var duplicates = [];
  14. for (var key in obj) duplicates.push(obj[key]); // &lt;-- here we go
  15. // probably you don&#39;t even need the array
  16. // 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

  1. var aLayer = app.activeDocument.activeLayer;
  2. var pageItemsRef = aLayer.pageItems;
  3. var duplicates = {};
  4. for (var i = 0, len = pageItemsRef.length; i < len; i++) {
  5. var currentItemName = pageItemsRef[i].name;
  6. if (duplicates[currentItemName] == null) {
  7. duplicates[currentItemName] = [];
  8. }
  9. duplicates[currentItemName].push({name: currentItemName, index: i});
  10. }
英文:

Try This"

  1. var aLayer = app.activeDocument.activeLayer;
  2. var pageItemsRef = aLayer.pageItems;
  3. var duplicates = {};
  4. for (var i = 0, len = pageItemsRef.length; i &lt; len; i++) {
  5. var currentItemName = pageItemsRef[i].name;
  6. if (duplicates[currentItemName] == null) {
  7. duplicates[currentItemName] = [];
  8. }
  9. duplicates[currentItemName].push({name: currentItemName, index: i});
  10. }

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:

确定