【问题标题】:Javascript group by array contentJavascript 按数组内容分组
【发布时间】:2011-11-25 21:47:23
【问题描述】:

我在弄清楚如何在 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 等,因此当通过它进行迭代时 array[i] 我将得到 A B C D 等

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

【问题讨论】:

  • 你说groupBy首先想到的是linq to js 可能是帮助linqjs.codeplex.com
  • 你能举个例子说明数组是什么样子的吗?

标签: javascript jquery arrays select


【解决方案1】:

假设您的数组定义如下:

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/

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

【讨论】:

  • 感谢 GregL 的回答。这也可以很好地工作。我的问题是“其他”数组是动态添加的。在这种情况下,我需要一种根据类别对数组进行分组的方法。有什么线索吗?
  • 它现在可以工作了!必须想办法将内容添加到数组中。非常感谢您的时间! :)
【解决方案2】:

如果 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>');
            }
        });
    }
);

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

【讨论】:

  • 非常感谢!这看起来很有希望。但是,如果我像这个 holderMap[$(this)[0]] 一样查看它,holderMap 是未定义的。那会是什么?
  • 查看您正在访问的变量的范围。在上面的例子中, holderMap 被定义为 new Array() 并且对变量的访问发生在函数内部。如果您想在不同的范围或函数中访问 holderMap,您可以将其声明为全局的。即高于 $(document).read();
猜你喜欢
  • 1970-01-01
  • 2012-10-04
  • 1970-01-01
  • 2015-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-23
相关资源
最近更新 更多