繁体   English   中英

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

[英]Is it possible to group this JSON data using 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 ,如果brgy_locat相同, countmedium countlowcountmediumcounthigh所有值相加。 像这样:

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

上面的值仅是示例。 看看我制作的这个JSFiddle

第一次,我误解了这个问题。 您仍然想展平,并且仍然可以使用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);
        }
    });

我看到了您的小提琴,您应该添加一个flatten函数,并在undefined current时使sum函数将其转换为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;
}

当您调用它传递您作为示例提供的数据时,结果将是您所要求的...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM