【问题标题】:How To create a JavaScript Multidimentional Array如何创建 JavaScript 多维数组
【发布时间】:2011-04-27 15:07:34
【问题描述】:

我需要动态创建一个Array,但是实在找不到解决办法……

基本上我需要:一个与类型和其中项目数相关联的 id。 然后对于每个 id,我需要添加可变数量的项目。

所以最后的例子必须是这样的:

id : 59 | type : combo_box | NbItem : 1
Item 1
name : text | value : test
name : icon | value : test.png

id : 60 | type : search_box | NbItem : 2
Item 1
name : text | value : Yahoo
name : icon | value : yahoo.png
name : weblink | value : yahoo.com

Item 2
name : text | value : Bing
name : icon | value : Bing.png
name : weblink | value : Bing.com

我再次指出它必须是动态的。我需要在执行过程中添加,比如array[60][name][0] = text

编辑

我正在尝试这样进行,但失败了:

var dropMenuArray;

var node = XMLDoc.getElementsByTagName("item")[itemCpt].getElementsByTagName("m_type")[0];
type = node.childNodes[0].nodeValue;

node = XMLDoc.getElementsByTagName("item")[itemCpt].getElementsByTagName("m_id")[XMLDoc.getElementsByTagName("item")[itemCpt].getElementsByTagName("m_id").length-1];
id = node.childNodes[0].nodeValue;

if ((type.indexOf('combo_button') != -1 && type.indexOf('combo_button_item') == -1) || type.indexOf('search_box') != -1) {
    dropMenuArray[id] = {
        Type: type,
        items: []
    };

    alert('Index : ' + id + '  -  Type : ' + type);
}

我的意思是没有警报,当我将数组创建放在commantary上时,我会弹出警报。

【问题讨论】:

  • 到目前为止你实际尝试过什么?
  • 您可能应该将每个组存储在一个对象中,然后拥有这些对象的数组。
  • +1 到Musual。如果您曾经用 PHP 编写过代码,那么您一定知道有两种类型的数组:索引数组和键值数组。在 JavaScript 中,键值数组被称为对象。当您使用 [name] 时,它可能就是您想要的。
  • 您能否详细说明您将如何处理这些数据?
  • 第 1 行:var dropMenuArray = [ ];

标签: javascript jquery arrays json multidimensional-array


【解决方案1】:

我认为你想要的是这样的数组:

var multi = [ { type: "combo_box", items: [ { name: "text", value: "Yahoo" } ] } ];

因此要添加一个新条目,您可以:

var multi[newIndex] = { type: "new type", items: [] };

向那个添加项目:

multi[newIndex].items.push({ name: "text", value: "Bing" });

您实际上并不需要显式存储项目的数量,因为 JavaScript" 将为您维护 "items" 列表的 "length" 属性。因此,

var howMany = multi[someIndex].items.length;

【讨论】:

  • 谢谢,这似乎是什么,我会试试这个!
  • 你可能想“alert()”“type”的值是什么。
  • 当我将 dropMenuArray 放在评论中时的第一个警报是:ID:49 - 类型:Search_box
  • 如果是带有大“S”的“Search_box”,您的代码只会检查带有小“s”的“search_box”...
  • 另外,看起来你可以只做简单的字符串比较而不是那些“indexOf()”调用,不是吗?
【解决方案2】:

你可以做这样的事情(使用对象):

var array = {
    59: {
        type: 'combo_box',
        NbItem: 1,
        name: ['text', 'test', 'icon']
    },
    60: {
        type: 'search_box',
        NbItem: 2,
        name: ['text', 'yahoo', 'weblink']
    },
}

//can also use array[60]['name'][1] below:
alert(array[60].name[1]); // will alert 'yahoo' 
array[60].name[1] = 'google';
alert(array[60].name[1]); // will alert 'google'

【讨论】:

    【解决方案3】:

    您可以使用关联数组:

    function Item(text,icon,weblink) {
     this.text = text;
     this.icon = icon;
     this.weblink = weblink;
    }
    
    var arr = new Array();
    
    var a = new Object();
    a["id"] = 59;
    a["type"] = "combo_box";
    var arr_items = new Array();
    arr_items.push( new Item("test","test.png") );
    arr_items.push( new Item("Yahoo", "yahoo.png", "yahoo.com") );
    a["items"] = arr_items;
    
    arr.push( a );
    
    ... //carry on with other objects
    

    【讨论】:

    • 上面的代码值得注意的是不是 JSON——不确定你是否可以在 JSON 中做任意数量的属性。
    【解决方案4】:

    只需将数组放入数组中... 但你可能更喜欢对象。

    这是您描述的结构,但我认为有更好的结构。

    [
        {
            "id": 59,
            "type": "combo_box",
            "items": [
                [
                    {
                        "name": "text",
                        "value": "test"
                    },
                    {
                        "name": "icon",
                        "value": "test.png"
                    }
                ]
            ]
        },
        {
            "id": 60,
            "type": "search_box",
            "items": [
                [
                    {
                        "name": "text",
                        "value": "Yahoo"
                    },
                    {
                        "name": "icon",
                        "value": "yahoo.png"
                    },
                    {
                        "name": "weblink",
                        "value": "yahoo.com"
                    }
                ],
                [
                    {
                        "name": "text",
                        "value": "Bing"
                    },
                    {
                        "name": "icon",
                        "value": "Bing.png"
                    },
                    {
                        "name": "weblink",
                        "value": "Bing.com"
                    }
                ]
            ]
        },
    ]
    

    NBItem的东西可以通过获取item数组的length属性来获得。

    这是可能更好的较短方式:

    [
        {
            "id": 59,
            "type": "combo_box",
            "items": [
                {
                    "text": "test",
                    "icon": "test.png"
                }
            ]
        },
        {
            "id": 60,
            "type": "search_box",
            "items": [
                {
                    "text": "Yahoo",
                    "icon": "yahoo.png",
                    "weblink": "yahoo.com"
                },
                {
                    "text": "Bing",
                    "icon": "Bing.png",
                    "weblink": "Bing.com"
                }
            ]
        },
    ]
    

    你可以做的最后一个改变是,如果元素的 id 是他在数组中的索引,你可以把它拿走,因为当你循环遍历数组时,你已经有了一个包含它的变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-19
      • 2012-05-14
      • 2012-10-29
      • 1970-01-01
      • 2015-11-15
      • 2018-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多