crossfilter / reductio

Reductio: Crossfilter grouping
Other
248 stars 42 forks source link

How to include constant string values to reduced values (Crossfilter and Reductio) #62

Closed EwetoyeIbrahim closed 4 years ago

EwetoyeIbrahim commented 4 years ago

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.

EwetoyeIbrahim commented 4 years ago

Thank you, I never knew it could be this cheap, valueList did the trick (SO link):

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.