数组变量在另一个数组变量中

发布于 2024-10-14 16:09:00 字数 469 浏览 0 评论 0 原文

抱歉,如果标题有点令人困惑。这就是我的意思 - 我有一个数组 和 和 对象。让我给出一个视觉效果:

这是我的对象:

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

Sorry if the title is a little confusing. Here's what I meant - I have an array and and object. Let me give a visual:

This is my object:

obj["id"] = ["1","2","3"];
obj["name"] = ["na","no","ne"];

my itemArray:

itemArray = ["id","name"];

And now, I want to retrieve the items in the object, I tried

for (var i:int = 0; i<objLen; i++)
{
    for (var j:int = 0; j<count; j++)
    {
        textBox_txt.appendText(obj.arr[i][j]);
    }
}

but it doesn't work. How do I actually use the arr[i] variable in the array? :s

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人海汹涌 2024-10-21 16:09:00

也许是这样的:

var array:Array;
var count:int;
for( var i:int = 0; i < itemArray.length; i++ )
{
    array = obj[ itemArray[ i ] ];
    count = array.length;
    for ( var j:int = 0; j<count; j++ )
    {
        textBox_txt.appendText( array[ j ] );
    }
}

Perhaps something like this:

var array:Array;
var count:int;
for( var i:int = 0; i < itemArray.length; i++ )
{
    array = obj[ itemArray[ i ] ];
    count = array.length;
    for ( var j:int = 0; j<count; j++ )
    {
        textBox_txt.appendText( array[ j ] );
    }
}
压抑⊿情绪 2024-10-21 16:09:00

好吧,我刚刚发现了这一点。 jeremyne​​albrown的答案有效,但还有另一种方法: http:// /www.kirupa.com/forum/showpost.php?p=2596002&postcount=2

var obj = new Object();
obj["id"] = ["1","2","3"];
obj["name"] = ["na","no","ne"];
obj["sasdf"] = "asdfa asdfasdf asdf asdf af";

for (var i in obj)
{
       if (obj[i] is Array)
       {
               for (var j:int = 0; j<obj[i].length; j++)
               {
                       trace(obj[i][j]);
                       //textBox_txt.appendText(obj.arr[i][j]);
               }

       }

}

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

var obj = new Object();
obj["id"] = ["1","2","3"];
obj["name"] = ["na","no","ne"];
obj["sasdf"] = "asdfa asdfasdf asdf asdf af";

for (var i in obj)
{
       if (obj[i] is Array)
       {
               for (var j:int = 0; j<obj[i].length; j++)
               {
                       trace(obj[i][j]);
                       //textBox_txt.appendText(obj.arr[i][j]);
               }

       }

}
酒儿 2024-10-21 16:09:00

本质上,使用两个 [ ] 运算符访问嵌套数组是正确的,但是您混淆了数组的组合方式:

  1. 您的第一个对象有两个命名属性,“id”和“name”,每个都包含一维数组

    var obj:Object = {
        id:[“1”,“2”,“3”],
        名称:[“na”,“no”,“ne”];
    }
    

    (我使用此表示法是为了使内容更具可读性 - 它创建与您的代码相同的对象。请注意 { } 和 [ ] 括号!)

  2. 你的 itemArray 是一个一维数组,里面只有两个字符串,但是它不会出现在代码中的任何其他地方。

    itemArray = ["id", "name"];
    
  3. 您的 for 循环迭代 obj 对象的属性“arr”,该属性不存在:

    for (var i:int = 0; i

现在忘记上面的一切。这是正确的做法:

  1. 如果您想要一个二维数组,您可以省略属性名称:

    var arr:数组= [ 
                       [“1”,“娜”], 
                       [“2”,“否”], 
                       [“3”,“ne”]
                   ];
    

    (注意:这有一组嵌套的 [ ] 运算符,没有 { } 括号!)
    并像这样迭代:

    for (var i:int = 0; i
  2. 您还可以使用命名属性来使事情看起来更漂亮,并且您的代码更具可读性:

    var itemArray:Array = [ 
        {id:"1", 姓名:"na"}, 
        {id:"2", 姓名:"否"}, 
        {id:"3", 姓名:"ne"} 
    ];
    

    (注意 [ ] 和 { }  括号的位置发生了变化!)

    这是一个包含 3 个对象的一维数组,每个对象都有属性“id”和“name”。然后,您可以迭代将各个对象名称附加到文本中,如下所示:

    foreach ( var obj:itemArray 中的对象) {
        textBox_txt.appendText(obj.id + "" + obj.name);
    } // 输出:1na2no3ne
    

Essentially, using two [ ] operators to access nested arrays is correct, but you are confusing the way the arrays are put together:

  1. Your first object has two named properties, "id" and "name", each contain one-dimensional arrays:

    var obj:Object = {
        id:["1", "2", "3"],
        name:["na", "no", "ne"];
    }
    

    ( I use this notation to make things more readable - it creates the same object as your code. Notice the { } and [ ] brackets!)

  2. Your itemArray is a one-dimensional array, which has only two strings inside, but it does not appear anywhere else in the code.

    itemArray = ["id", "name"];
    
  3. Your for loop iterates over the property "arr" of the obj Object, which does not exist:

    for (var i:int = 0; i<objLen; i++)
    {
        for (var j:int = 0; j<count; j++)
        {
            textBox_txt.appendText(obj.arr[i][j]); // obj has properties "id" and "name", but not "arr"
        }
    }
    

Now forget everything above. This is how you could do it right:

  1. If you wanted a two-dimensional array, you could omit the property names:

    var arr:Array= [ 
                       ["1", "na"], 
                       ["2", "no"], 
                       ["3", "ne"]
                   ];
    

    (Note: This has a nested sets of [ ] operators, no { } brackets!)
    and iterate like this:

    for (var i:int = 0; i<arr.length; i++)
    {
        for (var j:int = 0; j<arr[i].length; j++)
        {
            textBox_txt.appendText(arr[i][j]);
        }
    } // output: 1na2no3ne
    
  2. You could also use named properties to make things look nice, and your code more readable:

    var itemArray:Array = [ 
        {id:"1", name:"na"}, 
        {id:"2", name:"no"}, 
        {id:"3", name:"ne"} 
    ];
    

    (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:

    for each ( var obj:Object in itemArray) {
        textBox_txt.appendText(obj.id + "" + obj.name);
    } // output: 1na2no3ne
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文