使用js按多个属性分组的javascript对象

时间:2014-10-16 16:02:33

标签: javascript grouping

这是我的JavaScript对象。我想有一个方法来动态生成一个或多个属性分组的对象。参数attrs是一个数组,其中包含一些用于分组的属性。

var input= [
    {fistname:'Joe', age:'10', sex:'boy', class:'3'},
    {fistname:'Tom', age:'11', sex:'boy', class:'3'},
    {fistname:'Amily', age:'10', sex:'girl', class:'3'},
    {fistname:'Bob', age:'11', sex:'boy',class:'4'},
    {fistname:'Susan', age:'12', sex:'girl', class:'4'}
]

var attrs = ['age', 'class'];

function json2group(input, attrs){...}

结果:

[
    {
        label:'10',
        groups:[
            {
                label:'3',
                groups:[
                    {fistname:'Joe', age:'10', sex:'boy', class:'3'},
                    {fistname:'Amily', age:'10', sex:'girl', class:'3'}
                ]
            }
        ]
    },
    {
        label:'11',
        groups:[
            {
                label:'3',
                groups:[
                    {fistname:'Tom', age:'11', sex:'boy', class:'3'}
                ]
            },
            {
                label:'4',
                groups:[
                    {fistname:'Bob', age:'11', sex:'boy',class:'4'}
                ]
            }
        ]
    },
    {
        label:'12',
        groups:[
            {
                label:'4',
                groups:[
                    {fistname:'Susan', age:'12', sex:'girl', class:'4'}
                ]
            }
        ]
    }
]

请帮帮我。

3 个答案:

答案 0 :(得分:1)

您可以使用纯JS。

var input = [
    {fistname:'Joe', age:'10', sex:'boy', class:'3'},
    {fistname:'Tom', age:'11', sex:'boy', class:'3'},
    {fistname:'Amily', age:'10', sex:'girl', class:'3'},
    {fistname:'Bob', age:'11', sex:'boy',class:'4'},
    {fistname:'Susan', age:'12', sex:'girl', class:'4'}
];

var attrs = ['age', 'class'];

function GroupByArbitraryNumOfGroup(array, attrs) {
    var output = [];
    for (var i = 0; i < array.length; ++i) {
        var ele = array[i];
        var groups = output;
        for (var j = 0; j < attrs.length; ++j) {
            var attr = attrs[j];
            var value = ele[attr];
            var gs = groups.filter(function(g) {
                return g.hasOwnProperty('label') && g['label'] == value;
            });
            if (gs.length == 0) {
                var g = {};
                g['label'] = value;
                g['groups'] = [];
                groups.push(g);
                groups = g['groups'];
            } else {
                groups = gs[0]['groups'];
            }
        }
        groups.push(ele);
    }
    return output;
}

var o = GroupByArbitraryNumOfGroup(input, attrs);

您将获得o

[{
    'label': '10',
    'groups': [{
        'label': '3',
        'groups': [{
            'fistname': 'Joe',
            'age': '10',
            'sex': 'boy',
            'class': '3'
        }, {
            'fistname': 'Amily',
            'age': '10',
            'sex': 'girl',
            'class': '3'
        }]
    }]
}, {
    'label': '11',
    'groups': [{
        'label': '3',
        'groups': [{
            'fistname': 'Tom',
            'age': '11',
            'sex': 'boy',
            'class': '3'
        }]
    }, {
        'label': '4',
        'groups': [{
            'fistname': 'Bob',
            'age': '11',
            'sex': 'boy',
            'class': '4'
        }]
    }]
}, {
    'label': '12',
    'groups': [{
        'label': '4',
        'groups': [{
            'fistname': 'Susan',
            'age': '12',
            'sex': 'girl',
            'class': '4'
        }]
    }]
}]

答案 1 :(得分:0)

我找到了这个Underscore.js groupBy multiple values

它和我想要的几乎一样。使用&#39; lodash.groupBy&#39;也应该没事。

答案 2 :(得分:0)

您可以尝试使用Alasql库来准备复杂的结构。

这里我准备了一个有两个属性的例子:

var input= [ {fistname:'Joe', age:'10', sex:'boy', class:'3'},
             {fistname:'Tom', age:'11', sex:'boy', class:'3'} ];

function json2group(input, attr){
    var res1 = alasql('SELECT '+attr[0]+' AS label0,'+attr[1]+' AS label1, \
         ARRAY({label:'+attr[0]+',groups:_}) AS groups1 FROM ? GROUP BY label0,label1',[input]);
    return alasql('SELECT label0 AS label, ARRAY({label:label1,groups:groups1}) \
         AS groups FROM ? GROUP BY label', [res1]);
};

var res = json2group(input, ["age","class"]);

你可以试试in jsFiddle,它会产生你的结果。