Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

How to group a collection of data by its keys using Underscore.js?

Given the following data format, how can the data be grouped by their keys?

The format of the data is:

data=[
    {
        "billingid": 138,
        "amount": "800",
        "billamount": "200",
        "balance": "1",
        "paid": "810",
        "billdate": "Apr 2, 2014 12:00:00 AM",
        "tax": "11",
        "patientid": "TMCH17",
        "configid": 6,
        "paymenttype": "cash",
        "servicename": "Room",
        "firstname": "kannan"
    },
    {
        "billingid": 138,
        "amount": "800",
        "billamount": "500",
        "balance": "1",
        "paid": "810",
        "billdate": "Apr 2, 2014 12:00:00 AM",
        "tax": "11",
        "patientid": "TMCH17",
        "configid": 3,
        "paymenttype": "cash",
        "servicename": "Lab",
        "firstname": "kannan"
    },
    {
        "billingid": 138,
        "amount": "800",
        "billamount": "100",
        "balance": "1",
        "paid": "810",
        "billdate": "Apr 2, 2014 12:00:00 AM",
        "tax": "11",
        "patientid": "TMCH17",
        "configid": 1,
        "paymenttype": "cash",
        "servicename": "Consultation",
        "firstname": "kannan"
    }]

The expected output is:

 [{
        "billingid": 138,
        "amount": "800",
        bamount:[{
            "billamount": "100",
            "billamount": "200",
            "billamount": "300"
         }],
        "balance": "1",
        "paid": "810",
        "tax": "11",
        "patientid": "TMCH17",
        "configid": 1,
        "paymenttype": "cash",
        service[{
            "servicename": "Consultation",
            "servicename": "room",
            "servicename": "lab",
        }],
        "firstname": "kannan"
    }]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Pseudo Code

Here's the solution in general, for each object in the collection,

  1. Verify that each key does not exist in the result set.
  2. If the key exists and the result is not equal to the value, then
    1. Convert the value to an array and keep all unique values.
    2. Otherwise, set the value at this key to the value.

Problems

It's not defined in your question whether or not you want unique results. I suspect that the result set should not be unique for some keys. In which case, you should modify the logic within the each to reflect that logic. As an example, you probably want billamount to all duplication, but you want billingid to be unique.

Code

var summary = _.reduce(data, function(result, object) {

    _.each(object, function(value, key) {
        result[key] = ( _.has(result, key) && result[key] !== value ?
                      _.uniq(_.flatten([result[key], value])) : value);
    });

    return result;
} );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...