简体   繁体   English

Javascript按数组内容分组

[英]Javascript group by array content

I have some problems figuring out how to group to certain variables in javascript. 我在弄清楚如何分组到javascript中的某些变量时遇到一些问题。

Here is the deal. 这是交易。

I have one array containing categories in my case right now categories AZ but it could be anything (Animals - dogs - cats - etc). 我现在有一个数组,其中包含类别(现在是AZ),但它可以是任何东西(动物-狗-猫-等)。

In another array I have a result from an xml file with different content (title, content and category). 在另一个数组中,我得到了一个具有不同内容(标题,内容和类别)的xml文件的结果。 In my case the category containing letters from AZ corresponding to the category in my other array. 在我的情况下,类别包含来自AZ的字母,对应于我其他数组中的类别。

So what I want to do is first of all output one div for each category from the category array. 所以我想做的是首先为类别数组中的每个类别输出一个div。 When that is done I want to add inside each div the matching category items form my other array. 完成此操作后,我想在每个div内添加匹配的类别项,形成其他数组。

This is an example 这是一个例子

First output from my category array: 我的类别数组的第一个输出:

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

Second I want to add inside those divs the array objects that has a matching category inside them A, B, C etc. 其次,我想在这些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>

And so on... 等等...

Is this even possible? 这甚至可能吗?

EDIT: My First array only holds A, B, C, D, E etc so when iterating thru it array[i] i will get ABCD etc 编辑:我的第一个数组仅包含A,B,C,D,E等,因此在遍历数组时[i]我将得到ABCD等

My second array holds title category etc so if i want to have only the category i could iterate thru it like this arrMarkers[i].category. 我的第二个数组包含标题类别等,因此,如果我只想拥有类别,则可以像arrMarkers [i] .category这样遍历它。 That would output ex. 那将输出前。 AAABBEEFFF etc based on what categories the content of each array holds AAABBEEFFF等,根据每个数组的内容包含什么类别

Assuming you have your arrays defined something like this: 假设您的数组定义如下:

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'];

Then try the following jQuery code to create the divs: 然后尝试以下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');
    });
});

You can see a working example here: http://jsfiddle.net/greglockwood/8F8hv/ 您可以在此处看到一个有效的示例: http : //jsfiddle.net/greglockwood/8F8hv/

Let me know if you have any problems. 如果您有任何问题,请告诉我。

If the array from the xml can be set to a two dimensional one, things can go fine. 如果可以将xml中的数组设置为二维数组,那么一切就可以进行了。 Please look at his example: 请看他的例子:

html: HTML:

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

javascript/jQuery 使用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>');
            }
        });
    }
);

This code goes dynamic so that if a non categorized item is encountered, it skips automatically. 该代码是动态的,因此如果遇到未分类的项目,它将自动跳过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM