HPanpan

Js数组对象分组  编辑 


function test() {
        var list = [
            {"name": "John", "Average": 15, "High": 10, "DtmStamp": 1358226000000},
            {"name": "Jane", "Average": 16, "High": 92, "DtmStamp": 1358226000000},
            {"name": "Jane", "Average": 17, "High": 45, "DtmStamp": 1358226000000},
            {"name": "John", "Average": 18, "High": 87, "DtmStamp": 1358226000000},
            {"name": "Jane", "Average": 15, "High": 10, "DtmStamp": 1358226060000},
            {"name": "John", "Average": 16, "High": 87, "DtmStamp": 1358226060000},
            {"name": "John", "Average": 17, "High": 45, "DtmStamp": 1358226060000},
            {"name": "Jane", "Average": 18, "High": 92, "DtmStamp": 1358226060000}
        ];
 
        var sorted = this.groupBy(list, function (item) {
            return [item.name + item.High];
        });
        console.log(sorted);
    }
    test();
 
    function groupBy(array, f) {
        debugger;
        var groups = {};
        array.forEach(function (o) {
            var group = JSON.stringify(f(o));
            groups[group] = groups[group] || [];
            groups[group].push(o);
        });
        return Object.keys(groups).map(function (group) {
            return groups[group];
        });
    }