2

我正在研究一个 Javascript 函数,它接受一个 XML 文档并创建一个多维(根据需要)关联数组。在函数内部,数组正确构建,但是在返回 Array 对象时,它返回一个空数组。

有趣的是,如果我使用 push 方法,并以 {"index": index, "value":value} 的形式推送一个文字数组,而不是使用赋值运算符 (array[index]=value) 它工作得很好

为了进行测试,我使用了以下 XML 节点对象 (Level_1_node):

<Level_1>
    <Level_2>VALUE</Level_2>
</Level_1>

这是功能:

function get_array_from_XML(XML_node){
    var XML_array = new Array();
    var child_node;

    for(var i=0; i<XML_node.childNodes.length; i++){
        child_node = XML_node.childNodes[i];
        if(child_node.childNodes[0]){
            if (child_node.childNodes[0].nodeType == 3){
                XML_array[child_node.nodeName] = child_node.childNodes[0].nodeValue;
            } else {                
                XML_array[child_node.nodeName] = get_array_from_XML(child_node);
            }
        }
    }

    dump(XML_array);  //for my debugging, alerts "LEVEL_2 => VALUE", so everything seems fine

    return XML_array;
}

以下方法有效,但返回格式不理想:

function get_array_from_XML_using_push(XML_node){
    var XML_array = new Array();
    var child_node;

    for(var i=0; i<XML_node.childNodes.length; i++){
        child_node = XML_node.childNodes[i];
        if(child_node.childNodes[0]){
            if (child_node.childNodes[0].nodeType == 3){
                XML_array.push({
                    "index" : child_node.nodeName,
                    "value" : child_node.childNodes[0].nodeValue
                });
            } else {                
                XML_array.push({
                    "index" : child_node.nodeName,
                    "value" : get_array_from_XML_using_push(child_node)
                });
            }
        }
    }

    dump(XML_array);  //shows the fully populated array

    return XML_array;
}

现在当我运行get_array_from_XML(Level_1_node)它返回一个空数组,但get_array_from_XML_using_push(Level_1_node)返回

{0 => {"index" => "Level_2", "value" => "VALUE"}}

非常令人沮丧。欢迎任何见解。

4

1 回答 1

4

更改new Array()new Object()。构造Array函数不适用于关联数组;它仅适用于数字索引数组。对象本身在 JavaScript 中兼作关联数组。

function get_array_from_XML(XML_node){
    var XML_array = new Object();
    // -------------------^
    var child_node;

    for(var i=0; i<XML_node.childNodes.length; i++){
        child_node = XML_node.childNodes[i];
        if(child_node.childNodes[0]){
            if (child_node.childNodes[0].nodeType == 3){
                XML_array[child_node.nodeName] = child_node.childNodes[0].nodeValue;
            } else {                
                XML_array[child_node.nodeName] = get_array_from_XML(child_node);
            }
        }
    }

    dump(XML_array);  //for my debugging, alerts "LEVEL_2 => VALUE", so everything seems fine

    return XML_array;
}

使用对象作为关联数组

看看下面的例子:

var colors = new Object();
colors['red'] = '#f00';
colors['green'] = '#0f0';
colors['blue'] = '#00f';
colors['black'] = '#000';
colors['white'] = '#fff';

conosle.log(colors['red']); // => #f00
conosle.log(colors['green']); // => #0f0
conosle.log(colors['white']); // => #fff

// Use for..in or Object.keys to iterate over an object
for (var key in colors) {
    console.log(key, colors[key]);
}
// Logs all colors and their corresponding code

Object.keys(colors).forEach(function(key) {
    console.log(key, colors[key]);
});
// Also logs all colors and their corresponding code

请注意,colors['red'] = '#f00';与 相同colors.red = '#f00';。只有当您想要使用无效标识符的字符串或想要使用变量的值作为属性名称时,才真正需要方括号表示法。

于 2012-11-29T06:05:56.230 回答