【问题标题】:Photoshop JSX -- Using Split Method on Array, why does one not work and the other work?Photoshop JSX——在数组上使用拆分方法,为什么一个不起作用而另一个起作用?
【发布时间】:2011-01-26 21:48:18
【问题描述】:

我有这段代码,它会生成一系列关于在任何 Photoshop 文档中设置指南的信息。

var guides = app.activeDocument.guides;// get the current doc's guides

var guideArray = [];

for (var g = 0; g < guides.length; g++){
 guideArray.push( [guides[g].direction, guides[g].coordinate ]);// store the guide properties for later
}

prompt("title", guideArray);

提示符给出以下输出:

Direction.VERTICAL,47 px,Direction.VERTICAL,240 px,Direction.VERTICAL,182 px,Direction.VERTICAL,351 px,Direction.VERTICAL,119 px,Direction.VERTICAL,21 px,Direction.HORIZONTAL,89像素,Direction.HORIZONTAL,199 像素,Direction.HORIZONTAL,54 像素,Direction.HORIZONTAL,171 像素

我想通过添加这段代码来拆分这个数组

var b = [];

for (var i = 0; i < guideArray.length; i++){

 var b = guideArray[i].split(",");

}

这给了我这个错误,

exceptionMessage([Error:ReferenceError: guideArray[i].split 不是函数])

为什么?

忽略我正在做的事情的目的(已经以更优雅的方式弄清楚了),我很想知道为什么会失败。

我真的很好奇,因为我试过了,效果很好,

var guides = app.activeDocument.guides;// get the current doc's guides

var guideArray = [];

for (var g = 0; g < guides.length; g++){

 guideArray.push( [guides[g].direction, guides[g].coordinate ]);// store the guide properties for later

 }

var guideString = guideArray.toString();

var b = guideString.split("x,");

for (var i = 0; i < b.length; i++){

 var c = b[i].split(",");

 }

alert(c[1]);

这很有效,即使我在上面的 for 循环中使用 split 做看似相同的事情。

【问题讨论】:

    标签: split jsx


    【解决方案1】:

    我认为第二段代码有缺陷。它只有 c[0] 和 c[1] 的值。我认为这可能是因为我没有将它定义为一个数组,而是在我的 for 循环中不断地重新定义它。我不确定为什么 0 (Direction.VERTICAL) 和 1 (47 px) 有不同的值。

    所以这是我对我所写的第一个问题的修复。我只需要在我的循环中添加方法 .toString() ,就像这样。

    var guides = app.activeDocument.guides;// get the current doc's guides
    
    var guideArray = [];
    
    for (var g = 0; g < guides.length; g++){
     guideArray.push( [guides[g].direction, guides[g].coordinate ]);// store the guide properties for later
    }
    
    
    
    var b = [];
    
    for (var i = 0; i < guideArray.length; i++){
    
        b[i] = guideArray[i].toString().split(",");
    
    }
    

    现在数组 b 填充了预期的拆分结果。

    我猜 split 很挑剔,只能在字符串而不是数组元素上运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      • 2022-08-16
      • 1970-01-01
      相关资源
      最近更新 更多