0

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.

5
  • 1
    what have you actually tried so far? Commented Apr 27, 2011 at 15:09
  • You should probably be storing each group there in an object, then have an array of those objects. Commented Apr 27, 2011 at 15:14
  • +1 to Musual. If you have ever coded in PHP, you must know there are two types of arrays: indexed and keyvalue. In JavaScript, key-value arrays are called objects. And as you use [name] it is probably what you want. Commented Apr 27, 2011 at 15:24
  • Can you say a bit more about what you're going to do with that data? Commented Apr 27, 2011 at 15:55
  • Line 1: var dropMenuArray = [ ]; Commented Apr 27, 2011 at 19:57

4 Answers 4

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,

var howMany = multi[someIndex].items.length;
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks it seems to be what i what, i will try this !
Well you might want to "alert()" what the value of "type" is.
The first alert when i put the dropMenuArray in commentary is : ID : 49 - Type : Search_box
Well if it's "Search_box" with a big "S", your code just checks for "search_box" with a little "s" ...
Also, it looks like you could just do simple string comparisons instead of those "indexOf()" calls, no?
|
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'

Comments

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

1 Comment

the above code is worth noticing is NOT JSON -- not sure if you can do any number of attributes in JSON.
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.

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.