数组变量在另一个数组变量中
抱歉,如果标题有点令人困惑。这就是我的意思 - 我有一个数组 和 和 对象。让我给出一个视觉效果:
这是我的对象:
obj["id"] = ["1","2","3"];
obj["name"] = ["na","no","ne"];
我的 itemArray:
itemArray = ["id","name"];
现在,我想检索对象中的项目,我尝试过,
for (var i:int = 0; i<objLen; i++)
{
for (var j:int = 0; j<count; j++)
{
textBox_txt.appendText(obj.arr[i][j]);
}
}
但它不起作用。我如何实际使用数组中的 arr[i] 变量? :s
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许是这样的:
Perhaps something like this:
好吧,我刚刚发现了这一点。 jeremynealbrown的答案有效,但还有另一种方法: http:// /www.kirupa.com/forum/showpost.php?p=2596002&postcount=2
Ok, I just found this out. jeremynealbrown's answer works but there's also another way to go it: http://www.kirupa.com/forum/showpost.php?p=2596002&postcount=2
本质上,使用两个 [ ] 运算符访问嵌套数组是正确的,但是您混淆了数组的组合方式:
您的第一个对象有两个命名属性,“id”和“name”,每个都包含一维数组:
(我使用此表示法是为了使内容更具可读性 - 它创建与您的代码相同的对象。请注意 { } 和 [ ] 括号!)
你的 itemArray 是一个一维数组,里面只有两个字符串,但是它不会出现在代码中的任何其他地方。 。
您的 for 循环迭代 obj 对象的属性“arr”,该属性不存在:
现在忘记上面的一切。这是正确的做法:
如果您想要一个二维数组,您可以省略属性名称:
(注意:这有一组嵌套的 [ ] 运算符,没有 { } 括号!)
并像这样迭代:
您还可以使用命名属性来使事情看起来更漂亮,并且您的代码更具可读性:
(注意 [ ] 和 { } 括号的位置发生了变化!)
这是一个包含 3 个对象的一维数组,每个对象都有属性“id”和“name”。然后,您可以迭代将各个对象名称附加到文本中,如下所示:
Essentially, using two [ ] operators to access nested arrays is correct, but you are confusing the way the arrays are put together:
Your first object has two named properties, "id" and "name", each contain one-dimensional arrays:
( I use this notation to make things more readable - it creates the same object as your code. Notice the { } and [ ] brackets!)
Your itemArray is a one-dimensional array, which has only two strings inside, but it does not appear anywhere else in the code.
Your for loop iterates over the property "arr" of the obj Object, which does not exist:
Now forget everything above. This is how you could do it right:
If you wanted a two-dimensional array, you could omit the property names:
(Note: This has a nested sets of [ ] operators, no { } brackets!)
and iterate like this:
You could also use named properties to make things look nice, and your code more readable:
(Notice how the [ ] and { } brackets have changed places!)
This is a one-dimensional array containing 3 objects, each with properties "id" and "name". You could then iterate to append the individual object names to your text like this: