1

Given a data that looks like:

data = [
    {   
        "partner": "ABCD",
        "category": "CORTICOSTERIODS",
        "name": "NP Slow Meedrol Methylprednisolone Sodium Succinate Injection USP 500mg x 1 Vial",
        "price_sum": 22288.00,
        "qty_sum": 50.00000000000000000
    },
    {
        "partner": "ABCD",
        "category": "MEDICAL DISPOSABLE",
        "name": "4 INCH CAST FIBREGLASS",
        "price_sum": 135324.00,
        "qty_sum": 63.00000000000000000
    },
    {
        "partner": "ABCD",
        "category": "DERMATOLOGICALS",
        "name": "ABF CREAM 20G",
        "price_sum": 150169.29,
        "qty_sum": 150.00000000000000000
    },
    {   
        "partner": "CDEF",
        "category": "CORTICOSTERIODS",
        "name": "NP Slow Meedrol Methylprednisolone Sodium Succinate Injection USP 500mg x 1 Vial",
        "price_sum": 22288.00,
        "qty_sum": 50.00000000000000000
    },
    {
        "partner": "CDEF",
        "category": "MEDICAL DISPOSABLE",
        "name": "4 INCH CAST FIBREGLASS",
        "price_sum": 135324.00,
        "qty_sum": 63.00000000000000000
    }
]

I would like to add the category of the keys to the values when grouped by names alongside the sum of prices and qtys for further processing. With the help of crossfilter and reductio, I got what is consumable by other functions, just that I couldn't figure out how to add the category of each of the keys to it.

What I am currently using:

function categ_tab(data){
    // The needed data should be grouped by names summed up by qty and price
    // Thus crossfilter and reductio will be used to take the burden off server
    var group = crossfilter(data).dimension(function(d) { return d.name; }).group(),
        reducer = reductio();
    // sum up qty and price
    reducer.value("qty").sum(function(d) { return d.qty_sum; });
    reducer.value("price").sum(function(d) { return d.price_sum; });
    return reducer(group).all();

Current Result:

[
    {
        "key":"4 INCH CAST FIBREGLASS",
        "value":{
            "qty":{"sum":126},
            "price":{"sum":270648}
        }
    },
    {
        "key":"ABF CREAM 20G",
        "value":{
            "qty":{"sum":150},
            "price":{"sum":150169.29}
        }
    },
    {
        "key":"NP Slow Meedrol Methylprednisolone Sodium Succinate Injection USP 500mg x 1 Vial",
        "value":{
            "qty":{"sum":100},
            "price":{"sum":44576}
        }
    }
]

Desired Result:

[
    {
        "key":"4 INCH CAST FIBREGLASS",
        "value":{
            "qty":{"sum":126},
            "price":{"sum":270648},
            "category": {"iDontCare":"MEDICAL DISPOSABLE"}
        }
    },
    {
        "key":"ABF CREAM 20G",
        "value":{
            "qty":{"sum":150},
            "price":{"sum":150169.29},
            "category": {"iDontCare":"DERMATOLOGICALS"}
        }
    },
    {
        "key":"NP Slow Meedrol Methylprednisolone Sodium Succinate Injection USP 500mg x 1 Vial",
        "value":{
            "qty":{"sum":100},
            "price":{"sum":44576},
            "category": {"iDontCare":"CORTICOSTERIODS"}
        }
    }
]

Thank you.

I.Ewetoye
  • 183
  • 13
  • I don't understand the `iDontCare` field names in your example, but if you're looking for an array of the categories for each key, it seems like [valueList](https://github.com/crossfilter/reductio#reductiovaluelistaccessor) should do the job. – Gordon Sep 18 '20 at 11:01
  • Thank you @Gordon, `iDontCare` was just used to mean any name is okay by me. I have solved it with the valueList you referenced, I never believed it could be that cheap. Thank you. – I.Ewetoye Sep 18 '20 at 12:20

1 Answers1

1

Thank you @Gordon I never knew it could be this cheap, valueList did the trick:

function categ_tab(data){
    // The needed data should be grouped by names summed up by qty and price
    // Thus crossfilter and reductio will be used to take the burden off server
    var group = crossfilter(data).dimension(function(d) { return d.name; }).group(),
        reducer = reductio();
    // sum up qty and price
    reducer.value("qty").sum(function(d) { return d.qty_sum; });
    reducer.value("price").sum(function(d) { return d.price_sum; });
    reducer.value("category").valueList(function (d) { return d.category;});
    return reducer(group).all();

Gives:

[
    {
        "key":"4 INCH CAST FIBREGLASS",
        "value":{
            "qty":{"sum":126},
            "price":{"sum":270648},
            "category": {"valueList":["MEDICAL DISPOSABLE"]}
        }
    },
    {
        "key":"ABF CREAM 20G",
        "value":{
            "qty":{"sum":150},
            "price":{"sum":150169.29},
            "category": {"valueList":["DERMATOLOGICALS"]}
        }
    },
    {
        "key":"NP Slow Meedrol Methylprednisolone Sodium Succinate Injection USP 500mg x 1 Vial",
        "value":{
            "qty":{"sum":100},
            "price":{"sum":44576},
            "category": {"valueList":["CORTICOSTERIODS"]}
        }
    }
]

Thank you.

I.Ewetoye
  • 183
  • 13