是否可以使用underscore.js对此JSON数据进行分组?

时间:2015-03-12 05:33:21

标签: javascript json underscore.js

假设我有这些数据,它是查询的结果。

[
    [
        {
            "brgy_locat": "Kauswagan",
            "countlow": 8
        },
        {
            "brgy_locat": "Katugasan",
            "countlow": 24
        },
        {
            "brgy_locat": "Comagascas",
            "countlow": 3
        },
        {
            "counthigh": 7,
            "brgy_locat": "Barangay 9"
        },
        [
            {
                "brgy_locat": "Barangay 11",
                "countmedium": 1
            }
        ],
        [],
        [],
        [],
        [
            {
                "brgy_locat": "Mabini",
                "countmedium": 1
            }
        ],
        [
            {
                "counthigh": 27,
                "brgy_locat": "Barangay 6"
            },
            {
                "counthigh": 3,
                "brgy_locat": "Mabini"
            },
            {
                "counthigh": 2,
                "brgy_locat": "Barangay 2"
            },
            {
                "counthigh": 7,
                "brgy_locat": "Barangay 9"
            },
            {
                "counthigh": 17,
                "brgy_locat": "Comagascas"
            },
            {
                "counthigh": 1,
                "brgy_locat": "Tolosa"
            },
            {
                "counthigh": 33,
                "brgy_locat": "Barangay 7"
            }
        ]
    ]
]

我希望它按brgy_locat分组,如果countlow相同,则将countmediumcounthighbrgy_locat的所有值相加。 不知怎的,这样:

[
    {
        "brgy_locat": "Kauswagan",
        "countlow": 8,
        "countmedium": 1,
        "counthigh": 5
    }
]

以上值只是样本。看看我制作的JSFiddle

2 个答案:

答案 0 :(得分:1)

我第一次误解了这个问题。你仍然想要变平,你仍然可以使用groupby。我发现使用_.each和index参数在此非常有用。这应该适合你:

var temp = _.chain(data)
    .flatten()
    .groupBy('brgy_locat')
    .each(function(eachObj,index) { 
        if (!result.findWhere({ 'brgy_locat': index })) {
            var newObj = {
                'brgy_locat': index,
                countlow: _.reduce(eachObj, function(memo, obj) {
                    if (!obj.countlow) return memo;
                    return memo + obj.countlow;
                },0),
                countmedium: _.reduce(eachObj, function(memo, obj) {
                    if (!obj.countmedium) return memo;
                    return memo + obj.countmedium;
                },0),
                counthigh: _.reduce(eachObj, function(memo, obj) {
                    if (!obj.counthigh) return memo;
                    return memo + obj.counthigh;
                },0)
            };
            result.push(newObj);
        }
    });

答案 1 :(得分:1)

我看到了你的小提琴,你应该添加一个flatten函数,并使sum函数在current

时将undefined转换为0

您可以执行以下功能:

function sum(numbers) {
    return _.reduce(numbers, function (result, current) {
        return result + parseFloat(current || 0);
    }, 0);
}


function summarize(data) {
    var summary = _(data).chain()
    .flatten() 
    .groupBy("brgy_locat")
    .map(function (value, key) {
        return {
            brgy: key,
            low: sum(_(value).chain().pluck("countlow").value()),
            medium: sum(_(value).chain().pluck("countmedium").value()),
            high: sum(_(value).chain().pluck("counthigh").value())
        }
    })
    .value();

    return summary;
}

当你打电话给它传递你给出的数据时,结果将是你要求的......