0

I have a problem to create a specific array for one of my project. I need and array with always these fields :

[id | Type | nbItems]

And then on it :

m_name : m_value :

But this can be random, i can have 1 to infinite m_name/m_value.

[id | Type | nbItems] ->    m_name : weblink
                            m_value : http://xxx.com


[id | Type | nbItems] ->    m_name : text
                            m_value : Google

                            m_name : icon
                            m_value : icon.png

                            m_name : weblink
                            m_value : http://xxx.com

Hope it's clear enough...

EDIT

function readItem()
{
    var array = XMLDoc.getElementsByTagName("m_params")[curParam].getAttribute("enc:arraySize");

// m_params => nBItem

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

// type

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

// id

    var i;
    for (i = 0; i < array; i++) {
        readValue(i, XMLDoc);
    }

    itemCpt = itemCpt + i + 1;

    var child = XMLDoc.getElementsByTagName("m_childs")[curParam].getAttribute("enc:arraySize");
    curParam++;

    for (var c = 0; c < child; c++) {
        readItem(curParam, XMLDoc);
    }
} 

function readValue(i)
{

var item;

var node = XMLDoc.getElementsByTagName("m_params")[curParam].getElementsByTagName("m_name")[i];
item = node.childNodes[0].nodeValue;
// m_name

node = XMLDoc.getElementsByTagName("m_params")[curParam].getElementsByTagName("m_value")[i];
item = item.concat('\n' + node.childNodes[0].nodeValue);

// m_value

}

1 Answer 1

0

If you need to add the items with some known values you can try this:

var itemsList = [];

itemsList.push({
        id:1,
        Type:"Some",
        nbItems:[{
             m_name : "weblink",
             m_value : "http://xxx.com"
        }]
});


itemsList.push({
        id:2,
        Type:"AnotherSome",
        nbItems:[{
            m_name : "text",
            m_value : "Google"
        },{
            m_name : "icon",
            m_value : "icon.png"
        },{
            m_name : "weblink",
            m_value : "http://xxx.com"
        }]
});
2
  • Yeah but everythink has to be dynamic... Item also Commented Apr 20, 2011 at 14:39
  • How are you getting the item dynamically? Commented Apr 20, 2011 at 14:47

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.