Reputation: 10839
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.
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
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.
Upvotes: 0
Views: 394
Reputation: 8767
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.
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.
Upvotes: 0
Reputation: 6124
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
Upvotes: 0
Reputation: 413727
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,
var howMany = multi[someIndex].items.length;
Upvotes: 1
Reputation: 146310
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'
Upvotes: 0