开发者

Photoshop JSX -- Using Split Method on Array, why does one not work and the other work?

I have this code which generates an array of information on where the guidelines are set in any Photoshop document.

var guides = app.activeD开发者_如何学编程ocument.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);

And the prompt gives this output:

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 px,Direction.HORIZONTAL,199 px,Direction.HORIZONTAL,54 px,Direction.HORIZONTAL,171 px

I want to split this array with by adding this code

var b = [];

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

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

}

which gives me this error,

exceptionMessage([Error:ReferenceError: guideArray[i].split is not a function])

Why?

Ignoring purpose of what I'm doing (already figured it out in a more elegant manner), I am curious to know why this is failing.

I'm really curious because I tried this and it works,

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

And this works, even though I am doing seemingly the same thing with split in the for loop as above.


The second bit of code is flawed I think. It only has values for c[0] and c[1]. I think this is perhaps because I did not define it as an array, and am redefining it constantly in my for loop. I'm not sure why there are distinct values for 0 (Direction.VERTICAL) and 1 (47 px) though.

So here is my fix to the first problem I wrote about. I just needed to add the method .toString() in my loop, like so.

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(",");

}

Now array b is populated with the expected split results.

I'm guessing split is finicky and can only be run on a string not an array element.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜