English 中文(简体)
Photoshop JSX -- Using Split Method on Array, why does one not work and the other work?
原标题:
  • 时间:2011-01-26 21:48:18
  •  标签:
  • split
  • jsx

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

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

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.





相关问题
jQuery expander with max-lines property

I really thinks jQuery Expander plugin is great but it can t do satisfy my needs. It cuts text and put a "read more" button after the text which expands the text. But it only cuts when the text length ...

C# Regex.Split - Subpattern returns empty strings

Hey, first time poster on this awesome community. I have a regular expression in my C# application to parse an assignment of a variable: NewVar = 40 which is entered in a Textbox. I want my regular ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

split a string using a open and close tag

I wish to known if exist a clean way to split a string using different tags for opening and ending. For example: <&field1&>outside<&field2&> using the function split: ...

cut out part of a string

Say, I have a string "hello is it me you re looking for" I want to cut part of this string out and return the new string, something like s = string.cut(0,3); s would now be equal to: "lo is it me ...

How to split a string by spaces in a Windows batch file?

Suppose I have a string "AAA BBB CCC DDD EEE FFF". How can I split the string and retrieve the nth substring, in a batch file? The equivalent in C# would be "AAA BBB CCC DDD EEE FFF".Split()[n]

热门标签