数组在循环遍历时表现得很奇怪

发布于 2024-12-01 22:56:50 字数 943 浏览 0 评论 0原文

function showMyLayers() {

                            var Mylayers = [
                                    ["ISO"],
                                    ["ODO"],
                                    ["Black Round Mask"],
                                    ["red glow on gauges"],
                                    ["Compass"],
                                    ["4 Gauges"],
                                    ["Upper Stainless Steel"],
                                    ["Background"]
                            ];

                    for (x in Mylayers) {
                            var activelayer = app.activeDocument.layers[x];
                            activelayer.visible = true;
                            activelayer = null;
                    }
} showMyLayers();  

它出错了: [“4 Gauges”],

假设数字扰乱了数组。有什么想法为什么吗?这是extendscript,它是javascript,但适用于photoshop,以防您想知道。但它大部分基于 JS,所以它的行为应该像它一样。

function showMyLayers() {

                            var Mylayers = [
                                    ["ISO"],
                                    ["ODO"],
                                    ["Black Round Mask"],
                                    ["red glow on gauges"],
                                    ["Compass"],
                                    ["4 Gauges"],
                                    ["Upper Stainless Steel"],
                                    ["Background"]
                            ];

                    for (x in Mylayers) {
                            var activelayer = app.activeDocument.layers[x];
                            activelayer.visible = true;
                            activelayer = null;
                    }
} showMyLayers();  

it errors out on:
["4 Gauges"],

assuming the number is messing with the array. any ideas why? this is extendscript which is javascript but for photoshop in case you're wondering. but its based on JS for most part so it should behave like it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流年已逝 2024-12-08 22:56:50

for..in 循环用于迭代对象。要迭代数组,请使用经典的 for 循环。此外,你拥有的是一个数组的数组,这可能不是你想要的。

var Mylayers = [
    "ISO",
    "ODO",
    "Black Round Mask",
    "red glow on gauges",
    "Compass",
    "4 Gauges",
    "Upper Stainless Steel",
    "Background"
];

for (var i = 0; i < Mylayers.length; i++) {
    var x = Mylayers[i];
    var activelayer = app.activeDocument.layers[x];
    activelayer.visible = true;
    activelayer = null;
}

The for..in loop is to iterate over objects. To iterate over arrays, use a classic for loop. Further more, what you have there is an array of array, which is probably not what you want.

var Mylayers = [
    "ISO",
    "ODO",
    "Black Round Mask",
    "red glow on gauges",
    "Compass",
    "4 Gauges",
    "Upper Stainless Steel",
    "Background"
];

for (var i = 0; i < Mylayers.length; i++) {
    var x = Mylayers[i];
    var activelayer = app.activeDocument.layers[x];
    activelayer.visible = true;
    activelayer = null;
}
长途伴 2024-12-08 22:56:50

啊... Adob​​e JavaScript...

您的问题是您实际上是在循环数组的索引,而不是值。因此,您可能有一个层 0...4,但层 5 可能是一个文件夹。

建议:

  1. 您使用嵌套数组而不是字符串。我会去掉数组中项目周围的 []
  2. 不要使用 for( x in Mylayers ) 然后使用 x,而是使用 for(x in Mylayers){ var tmp = Mylayers[x]
  3. 我不确定 ExtendScript,但是在JSFL(相当于Flash)中,访问图层对象的方法是layers[timeline.getLayerIndex(layerName)]

Ah... Adobe JavaScript...

Your issue is that you're actually looping through the indexes of the array, and not the values. So, you probably have a layer 0...4, but layer 5 is probably a folder.

Recommendations:

  1. You're using nested arrays instead of strings. I would get rid of the [] around the items in the array.
  2. Instead of for( x in Mylayers ) and then using x, use for(x in Mylayers){ var tmp = Mylayers[x]
  3. I am not certain about ExtendScript, but in JSFL (the equivalent for Flash) the way to get access to a layer object was layers[timeline.getLayerIndex(layerName)]
脸赞 2024-12-08 22:56:50

我不认为你在做你认为你在做的事情。 Javascript 中的 for..in 循环不是您可能习惯的 foreach 循环,并且在大多数情况下对于循环数组来说是一个糟糕的选择。它将 x 设置为对象中的下一个键,而不是将 x 设置为数组中的实例元素。在本例中,为数值 (0, 1, 2...)。不幸的是,它还会循环遍历 Array 实例上的对象属性(length、forEach 等)。

您确实应该使用常规的 for 循环:

var MyLayers = [ 'foo', 'bar', 'baz' ];

for(var i = 0; i < MyLayers.length; i += 1) {
   app.activeDocument.layers[MyLayers[i]].visible = true;
}

您也不需要显式地将内容设置为 null。 JavaScript 是内存管理的。

I don't think you're doing what you think you're doing. The for..in loop in Javascript isn't a foreach loop that you might be used to, and in most cases is a poor choice for looping over arrays. Instead of setting x to an instance element in the array, it sets x to the next key in the object. In this case, a numeric value (0, 1, 2...). Unfortunately, it will also loop over object properties on your Array instance (length, forEach, etc).

You really should be using a regular for loop:

var MyLayers = [ 'foo', 'bar', 'baz' ];

for(var i = 0; i < MyLayers.length; i += 1) {
   app.activeDocument.layers[MyLayers[i]].visible = true;
}

You also don't need to explicitly set things to null. Javascript is memory managed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文