按数组内容的Javascript组

时间:2011-09-27 09:59:03

标签: javascript jquery arrays select

我在确定如何在javascript中对某些变量进行分组时遇到了一些问题。

这是合约。

我有一个包含类别的数组,就我现在的类别A-Z而言,它可能是任何东西(动物 - 狗 - 猫 - 等)。

在另一个数组中,我得到了一个包含不同内容(标题,内容和类别)的xml文件的结果。在我的例子中,包含A-Z字母的类别对应于我的另一个数组中的类别。

所以我想做的是首先从类别数组输出每个类别的一个div。完成后,我想在每个div中添加匹配的类别项,形成我的另一个数组。

这是一个例子

我的类别数组的第一个输出:

<div id="A"></div>
<div id="B"></div>
<div id="C"></div>
<div id="D"></div>

其次我想在这些div中添加具有匹配类别的数组对象,包括A,B,C等。

<div id="A">
<div id="contentFromMyOtherArray"> this object comes from my other array and had the content category A </div>
<div id="contentFromMyOtherArray"> this object also comes from my other array and had the content category A still inside the A div </div>
</div>
<div id="B">
<div id="contentFromMyOtherArray"> this object comes from the array and had the content category B </div>
</div>

等等......

这甚至可能吗?

修改 我的第一个数组只保存A,B,C,D,E等,所以当通过它迭代时,我会得到A B C D等

我的第二个数组包含标题类别等,所以如果我想只有类别,我可以像这个arrMarkers [i] .category一样迭代它。那将输出ex。 A A B B E E F F F等基于每个数组的内容所包含的类别

2 个答案:

答案 0 :(得分:3)

假设您的数组定义如下:

var categories = ['A', 'B', 'C', 'D'];
var other = [];
other['A'] = ['item 1', 'item 2', 'item 3'];
other['B'] = ['item 4', 'item 5'];
other['C'] = ['item 6'];

然后尝试以下jQuery代码来创建div:

$(document).ready(function() {    
    $.each(categories, function(index, category) {
        var categoryId = category.replace(/ /gi, '_');
        var newCategory = $('<div />').attr('id', categoryId);
        // you can set any other properties on this new div here using .attr()
        $.each(other[category], function(index, item) {
            var itemId = category + '_' + item.replace(/ /gi, '_');
            var newInnerDiv = $('<div />').attr('id', itemId);
            // set any other properties like text content, etc here
            newInnerDiv.appendTo(newCategory);
        });
        newCategory.appendTo('body');
    });
});

您可以在此处查看一个有效的示例:http://jsfiddle.net/greglockwood/8F8hv/

如果您有任何问题,请告诉我。

答案 1 :(得分:0)

如果xml中的数组可以设置为二维数组,那么事情就可以了。请看他的例子:

HTML:

<div id="containerDiv">
    <div id="A"></div>
    <div id="B"></div>
    <div id="C"></div>
</div>

的JavaScript / jQuery的

var resultArray = [
    ["A", "Title1", "this object comes from my other array and had the content category A"],
    ["B", "Title2", "this object comes from my other array and had the content category B"],
    ["C", "Title3", "this object comes from my other array and had the content category C"],
    ["D", "Title4", "this object comes from my other array and had the content category D"],
    ["A", "Title5", "this object comes from my other array and had the content category A"],
    ["A", "Title6", "this object comes from my other array and had the content category A"],
    ["C", "Title7", "this object comes from my other array and had the content category C"],
    ["D", "Title8", "this object comes from my other array and had the content category D"],
    ["C", "Title9", "this object comes from my other array and had the content category C"],
    ["D", "Title10", "this object comes from my other array and had the content category D"],
    ["A", "Title11", "this object comes from my other array and had the content category A"],
    ["D", "Title12", "this object comes from my other array and had the content category D"],
    ["C", "Title13", "this object comes from my other array and had the content category C"],
    ["B", "Title14", "this object comes from my other array and had the content category B"]
];

$(document).ready(
    function() {
        var holderMap = new Array();
        $("div#containerDiv").children("div").each(
            function() {
                holderMap[$(this).attr("id")]=$(this);
            }
        );
        var elem = null;
        var value = null;
        $(resultArray).each(function() {
            elem = holderMap[$(this)[0]];
            value = $(this)[2];
            if(elem) {
                $(elem).html($(elem).html() + '<div id="contentFromMyOtherArray">' + value + '</div>');
            }
        });
    }
);

此代码是动态的,因此如果遇到未分类的项目,它会自动跳过。