简体   繁体   中英

Is it possible to group this JSON data using underscore.js?

Let's say I have this data, it is the result from a query.

[
    [
        {
            "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"
            }
        ]
    ]
]

I wanted it to be group by brgy_locat and sum all the values from countlow , countmedium and counthigh if brgy_locat is the same. Somehow like this:

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

Values above are just samples. Take a look at this JSFiddle I made.

I misunderstood the question the first time around. You still want to flatten, and you can still use groupby. I found that using _.each and the index argument is very useful in this. This should do it for you:

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);
        }
    });

I saw your fiddle, and you should add a flatten function, and make your sum function transform the current to 0 when it is undefined

You can do a function like that:

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;
}

When you call it passing the data you gave as example, the result will be what you asked for...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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