I need to create dynamically an Array, but i really can't find a solution...
我需要动态创建一个数组,但我真的找不到解决方案......
Basically what i need : An id linked with a type and the number of items in it. Then for each id i need to add a variable number of item.
基本上我需要的是:一个与一个类型和它中的项目数量相关联的id。然后对于每个id我需要添加一个可变数量的项目。
So the final example have to be like this :
所以最后的例子必须是这样的:
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
I precise once again that it have to be dynamic. I need to add during the execution, like array[60][name][0] = text
我再次确切地说它必须是动态的。我需要在执行期间添加,如array [60] [name] [0] = text
EDIT
I'm trying to proceed like this, but it fail :
我试图这样做,但它失败了:
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);
}
I mean no alert, and when i put the array creation on commantary i have the alert popup.
我的意思是没有警报,当我把数组创建放在commantary上时,我有警告弹出窗口。
4 个解决方案
#1
1
I think what you want is an array like this:
我想你想要的是这样一个数组:
var multi = [ { type: "combo_box", items: [ { name: "text", value: "Yahoo" } ] } ];
Thus to add a new entry, you'd do:
因此,要添加新条目,您需要:
var multi[newIndex] = { type: "new type", items: [] };
To add items to that one:
要向该项目添加项目:
multi[newIndex].items.push({ name: "text", value: "Bing" });
You don't really need to explicitly store the number of items, since JavaScript" will maintain the "length" property of the "items" list for you. Thus,
您实际上并不需要显式存储项目数,因为JavaScript“将为您保留”项目“列表的”长度“属性。因此,
var howMany = multi[someIndex].items.length;
#2
0
you can do something like this (using objects):
你可以做这样的事情(使用对象):
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
0
You can use associative arrays:
您可以使用关联数组:
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
#4
0
Just put arrays in arrays... But you might prefer objects.
只需将数组放入数组......但您可能更喜欢对象。
This is the structure you described but I think there's a better one.
这是你描述的结构,但我认为有一个更好的结构。
[
{
"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"
}
]
]
},
]
The NBItem thing can be obtained by getting the length property of the item array.
可以通过获取项数组的length属性来获取NBItem事物。
This is the shorter way that might be better:
这是可能更好的更短的方式:
[
{
"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"
}
]
},
]
A last change you could make is, if the id of the elements is his index in the array, you could just take it away because when you'll loop through the array, you'll already have a variable containing it.
你可以做的最后一个改变是,如果元素的id是数组中的索引,你可以把它拿走,因为当你循环遍历数组时,你已经有了一个包含它的变量。
#1
1
I think what you want is an array like this:
我想你想要的是这样一个数组:
var multi = [ { type: "combo_box", items: [ { name: "text", value: "Yahoo" } ] } ];
Thus to add a new entry, you'd do:
因此,要添加新条目,您需要:
var multi[newIndex] = { type: "new type", items: [] };
To add items to that one:
要向该项目添加项目:
multi[newIndex].items.push({ name: "text", value: "Bing" });
You don't really need to explicitly store the number of items, since JavaScript" will maintain the "length" property of the "items" list for you. Thus,
您实际上并不需要显式存储项目数,因为JavaScript“将为您保留”项目“列表的”长度“属性。因此,
var howMany = multi[someIndex].items.length;
#2
0
you can do something like this (using objects):
你可以做这样的事情(使用对象):
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
0
You can use associative arrays:
您可以使用关联数组:
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
#4
0
Just put arrays in arrays... But you might prefer objects.
只需将数组放入数组......但您可能更喜欢对象。
This is the structure you described but I think there's a better one.
这是你描述的结构,但我认为有一个更好的结构。
[
{
"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"
}
]
]
},
]
The NBItem thing can be obtained by getting the length property of the item array.
可以通过获取项数组的length属性来获取NBItem事物。
This is the shorter way that might be better:
这是可能更好的更短的方式:
[
{
"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"
}
]
},
]
A last change you could make is, if the id of the elements is his index in the array, you could just take it away because when you'll loop through the array, you'll already have a variable containing it.
你可以做的最后一个改变是,如果元素的id是数组中的索引,你可以把它拿走,因为当你循环遍历数组时,你已经有了一个包含它的变量。