如何在不使用循环的情况下将简单的 XMLList 转换为字符串数组?
如何在不使用循环的情况下将以下 XMLList 转换为字符串数组?
<labels>
<label>All</label>
<label>your</label>
<label>base</label>
<label>are</label>
<label>belong</label>
<label>to</label>
<label>us.</label>
</labels>
我想要这个结果:
["All","your","base","are","belong","to","us."]
现在,我正在执行以下操作:
var labelsArray:Array /* of String */ = [];
for each (var labelText:String in labels.label)
{
labelsArray.push(labelText);
}
我想知道在 ActionScript 3.0 中是否有更简单的方法来执行此操作
How can I convert the following XMLList to an Array of Strings without using a loop?
<labels>
<label>All</label>
<label>your</label>
<label>base</label>
<label>are</label>
<label>belong</label>
<label>to</label>
<label>us.</label>
</labels>
I want this result:
["All","your","base","are","belong","to","us."]
Right now, I am doing the following:
var labelsArray:Array /* of String */ = [];
for each (var labelText:String in labels.label)
{
labelsArray.push(labelText);
}
I was wondering if there is a simpler way to do this in ActionScript 3.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这很有效,但使用了一些奇怪的 XMLList 语法。 如果需要,最后一条语句可以放在一行上。
toString() 调用可以替换为 attribute() 调用以提取属性。
This works good but uses some odd syntax of the XMLList. The last statement can be placed on one line if desired.
The toString() call can be replaced with an attribute() call to pull out attributes.
这个效果很好:
This one works pretty well:
您当前的实现已经足够了。 您可以进行的唯一优化(尽管除非您使用 Vector,否则我不会打扰。<>)是将初始容量传递到 Array 构造函数中:
Your current implementation is more than sufficient. The only optimisation you could make (though I wouldn't bother unless you are using Vector.<>) is to pass in the initial capacity into the Array constructor:
尽管认真使用了
for
循环并逻辑地处理给定的XML
对象,但这仍然是XMLList
的工作。它最好看起来像这样:
这将输出:
我自己在闪存中确认了这一点。 就我个人而言,使用可选行和引用标签[0]等对我来说是有意义的,但这里不需要这样做。
我知道您要求一个字符串数组作为输出,但基本上我是在问您为什么不能只使用 XMLList 对象的数组访问器。
这里有一个很好的演练:E4X 上的 Senulous。
Despite the earnest uses of
for
loops and logically working on theXML
object as given, this is a job forXMLList
.It would best look something like this:
This would output:
I've confirmed this in flash myself. Personally it makes sense to me to use the optional line, and reference
labels[0]
etc. but that's not needed here.I know you're asking for an array of strings as the output, but basically I'm asking you why you can't just use the array accessors of an XMLList object.
Here's a fine walk-through on that: Senocular on E4X.
for
循环在 AS 中非常快。 你为什么需要那个? 但你可以尝试一下:for
loops are extremely fast in AS. Why'd you need that? But you could give this a try:我觉得在某个地方有一个关于这个的俏皮话……哦,好吧。
问题:为什么 .length 在这里失败? (始终为 0)
i feel like there's a one-liner for this out there somewhere... oh well.
question: why does .length fail here? (always 0)