Javascript 在循环中向数组添加变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7638658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
add variable to array in a loop
提问by Lukasz
using extend script to push a variable into an array it's basically javascript. any idea what I am doing wrong?
使用扩展脚本将变量推送到数组中,它基本上是 javascript。知道我做错了什么吗?
if ( app.documents.length > 0 ) {
for ( i = 0; i< app.activeDocument.textFrames.length; i++) {
var allSizes = []; //set up empty array
textArtRange = app.activeDocument.textFrames[i].textRange;
var fontName = textFonts.getByName("Nobile");
alert (fontName);
textArtRange.characterAttributes.textFont = fontName;
var fontSizes = textArtRange.characterAttributes.size;
allSizes.push(fontSizes)
alert (fontSizes);
}
alert (allSizes);
}
the alerts for allSizes only return single values, not the array.
allSizes 的警报只返回单个值,而不是数组。
回答by Rob W
Move the definition of allSizes = []
outside the loop.
将定义allSizes = []
移到循环外。
Currently, you're "resetting" the value of allSizes
at each loop.
目前,您正在“重置”allSizes
每个循环的值。
回答by Chris Eberle
You're setting up the empty array inside of the for loop. It's resetting it each time. Move it above the for loop:
您正在 for 循环内设置空数组。每次都在重置。将其移至 for 循环上方:
var allSizes = []; //set up empty array
for ( i = 0; i< app.activeDocument.textFrames.length; i++) {
.....