Let's say I have the following for loop in Javascript (this is for Adobe Photoshop using ExtendScript):
Would it be possible to rewrite this so that all six of the created created text fields are all assigned as six different variables, each with a constant name only differing by "i" as specified in the for loop? Furthermore, would it then be possible to assign all of those variables into an array within the for loop instead of writing out each variable name in the array individually? If so, how would you go about this?
-
Please copy the code and paste it here using the proper formatting to help other users understand what you're trying to achieve.Kevin– Kevin2016年10月21日 11:41:30 +00:00Commented Oct 21, 2016 at 11:41
1 Answer 1
You can create window variables window["num"] = "1" == var num = "1"
var p = ["I","You","We","They"];
for(var i=0;i<p.length;i++){
window[p[i]] = p[i]+" "+"Love Cacke";
}
//now you have variables I,You,We,They
console.log(I);
console.log(You);
console.log(We);
console.log(They);
/*
[
"I Love Cacke",
"You Love Cacke",
"We Love Cacke",
"They Love Cacke"
]
*/