开发者

Dynamically create jS/jQuery Multiple Array

开发者 https://www.devze.com 2023-02-27 17:34 出处:网络
I have a problem to create a specific array for one of my project. I need and array with always these fields :

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.g开发者_开发问答etElementsByTagName("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

}


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"
        }]
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号