最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How to group by columns in javascript array - Stack Overflow

programmeradmin1浏览0评论

I need to group element of a multidimensional array in javascript. This is my array:

        var data = []; 
        data.push(["ASIA", "CHINA", 1992, 31689222674, 5]); 
        data.push(["ASIA", "CHINA", 1997, 31327384072, 5]); 
        data.push(["ASIA", "INDIA", 1992, 27910388563, 5]); 
        data.push(["ASIA", "INDIA", 1993, 28669881069, 5]); 
        data.push(["ASIA", "INDIA", 1997, 29421629324, 5]); 
        data.push(["ASIA", "INDONESIA", 1992, 30990831267, 5]); 
        data.push(["ASIA", "INDONESIA", 1997, 31478506122, 5]); 
        data.push(["ASIA", "JAPAN", 1992, 29239959577, 5]); 
        data.push(["ASIA", "JAPAN", 1996, 29901316952, 5]); 
        data.push(["ASIA", "JAPAN", 1997, 29860665098, 5]); 
        data.push(["ASIA", "VIETNAM", 1992, 27229816811, 5]); 
        data.push(["ASIA", "VIETNAM", 1996, 27553006375, 5]); 
        data.push(["ASIA", "VIETNAM", 1997, 27240345298, 5]); 

        data.push(["AMERICA", "BRAZIL", 1992, 31689222674, 5]); 
        data.push(["AMERICA", "BRAZIL", 1993, 31207214888, 5]); 
        data.push(["AMERICA", "BRAZIL", 1994, 31926517793, 5]); 
        data.push(["AMERICA", "EUA", 1992, 27910388563, 5]); 
        data.push(["AMERICA", "EUA", 1993, 28669881069, 5]); 
        data.push(["AMERICA", "EUA", 1994, 29343326950, 5]); 

I need to group elements by bining the contents of that array. I found this code, but it considers an array index.

function group(data, index) {
    var o;
    var other = {};
    $.each(data, function(i, value) {
        o = data[i][index];         
        if (!(o in other))
            other[o] = [];
        other[o].push(data[i]);
    })
    return other;           
}

Then the group with more indices should be as an example: grouping by index 1 + 2.

    ASIA - CHINA => ["ASIA", "CHINA", 1992, 31689222674, 5], 
                    ["ASIA", "CHINA", 1997, 31327384072, 5];
    ASIA - INDIA => ....
    ASIA - INDONESIA => .....
    ASIA - JAPAN => ....
    ASIA - VIETNAM =>

    AMERICA - BRAZIL => ....
    AMERICA - EUA => ....

This should also work for group involving more indices as well.

I need to group element of a multidimensional array in javascript. This is my array:

        var data = []; 
        data.push(["ASIA", "CHINA", 1992, 31689222674, 5]); 
        data.push(["ASIA", "CHINA", 1997, 31327384072, 5]); 
        data.push(["ASIA", "INDIA", 1992, 27910388563, 5]); 
        data.push(["ASIA", "INDIA", 1993, 28669881069, 5]); 
        data.push(["ASIA", "INDIA", 1997, 29421629324, 5]); 
        data.push(["ASIA", "INDONESIA", 1992, 30990831267, 5]); 
        data.push(["ASIA", "INDONESIA", 1997, 31478506122, 5]); 
        data.push(["ASIA", "JAPAN", 1992, 29239959577, 5]); 
        data.push(["ASIA", "JAPAN", 1996, 29901316952, 5]); 
        data.push(["ASIA", "JAPAN", 1997, 29860665098, 5]); 
        data.push(["ASIA", "VIETNAM", 1992, 27229816811, 5]); 
        data.push(["ASIA", "VIETNAM", 1996, 27553006375, 5]); 
        data.push(["ASIA", "VIETNAM", 1997, 27240345298, 5]); 

        data.push(["AMERICA", "BRAZIL", 1992, 31689222674, 5]); 
        data.push(["AMERICA", "BRAZIL", 1993, 31207214888, 5]); 
        data.push(["AMERICA", "BRAZIL", 1994, 31926517793, 5]); 
        data.push(["AMERICA", "EUA", 1992, 27910388563, 5]); 
        data.push(["AMERICA", "EUA", 1993, 28669881069, 5]); 
        data.push(["AMERICA", "EUA", 1994, 29343326950, 5]); 

I need to group elements by bining the contents of that array. I found this code, but it considers an array index.

function group(data, index) {
    var o;
    var other = {};
    $.each(data, function(i, value) {
        o = data[i][index];         
        if (!(o in other))
            other[o] = [];
        other[o].push(data[i]);
    })
    return other;           
}

Then the group with more indices should be as an example: grouping by index 1 + 2.

    ASIA - CHINA => ["ASIA", "CHINA", 1992, 31689222674, 5], 
                    ["ASIA", "CHINA", 1997, 31327384072, 5];
    ASIA - INDIA => ....
    ASIA - INDONESIA => .....
    ASIA - JAPAN => ....
    ASIA - VIETNAM =>

    AMERICA - BRAZIL => ....
    AMERICA - EUA => ....

This should also work for group involving more indices as well.

Share Improve this question asked Mar 3, 2012 at 0:50 Anderson CarnielAnderson Carniel 1,7812 gold badges16 silver badges22 bronze badges 1
  • 5 Are you asking a question, or giving instructions? – user1106925 Commented Mar 3, 2012 at 0:59
Add a ment  | 

3 Answers 3

Reset to default 10

If you need this kind of functionality, underscore.js is highly remended.

var groupedData = _.groupBy(data, function(innerArray) {
    return innerArray[0] + "-" + innerArray[1];
});

You could also customize the function

function groupByTwoIndexes(data, index1, index2) {
    var o;
    var other = {};
    $.each(data, function(i, value) {
        o = data[i][index1] + "-" + data[i][index2];         
        if (!(o in other))
            other[o] = [];
        other[o].push(data[i]);
    })
    return other;           
}

var groupedDataByTwo = groupByTwoIndexes(data, 0, 1);

Demo

If you are familiar with .NET's Linq you might find it interesting to use LinqJS - http://linqjs.codeplex./

It claims to support all Linq's methods including GroupBy.

I just edited the code above to

function group(data, index1, index2) 
{

var o;

var other = {};

$.each(data, function(i, value  
 {
    o = data[i][index1][index2];         
    if (!(o in other))
        other[o] = [];
    other[o].push(data[i]);
})
return other;           

}

var result = group(data,0,1);

so now the array will be grouped by index1 and index2 which are continent and country. I have tested and it is working.

发布评论

评论列表(0)

  1. 暂无评论