I have an array of data that looks something like this
{
"data":[
{
"date":"2022-02-10",
"date_ts":1644440400,
"month":2,
"week":6,
"sub1":"sm",
"sub2":"d0q2",
"sub3":"",
"sub4":"",
"cost":8531.94,
"sms_count":3102,
"payout":29750.0,
"net_margin":21218.06
},
{
"date":"2022-02-10",
"date_ts":1644440400,
"month":2,
"week":6,
"sub1":"sm",
"sub2":"d0q1",
"sub3":"data_check",
"sub4":"",
"cost":5499.16,
"sms_count":1999,
"payout":12885.0,
"net_margin":7385.84
},
{
"date":"2022-02-10",
"date_ts":1644440400,
"month":2,
"week":6,
"sub1":"sm",
"sub2":"d1q1",
"sub3":"vitrina",
"sub4":"",
"cost":8994.3,
"sms_count":3270,
"payout":12748.0,
"net_margin":3753.7
},
{
"date":"2022-02-10",
"date_ts":1644440400,
"month":2,
"week":6,
"sub1":"sm",
"sub2":"d0q1",
"sub3":"webbdata_check",
"sub4":"",
"cost":4529.32,
"sms_count":1647,
"payout":14280.0,
"net_margin":9750.68
},
{
"date":"2022-02-10",
"date_ts":1644440400,
"month":2,
"week":6,
"sub1":"sm",
"sub2":"d0q3",
"sub3":"",
"sub4":"",
"cost":8537.82,
"sms_count":3104,
"payout":7140.0,
"net_margin":-1397.82
},
{
"date":"2022-02-10",
"date_ts":1644440400,
"month":2,
"week":6,
"sub1":"sm",
"sub2":"d1q2",
"sub3":"bounce",
"sub4":"",
"cost":2559.94,
"sms_count":930,
"payout":4707.0,
"net_margin":2147.06
},
{
"date":"2022-02-10",
"date_ts":1644440400,
"month":2,
"week":6,
"sub1":"sm",
"sub2":"d0q1",
"sub3":"random",
"sub4":"",
"cost":8476.62,
"sms_count":3082,
"payout":50616.0,
"net_margin":42139.38
},
{
"date":"2022-02-10",
"date_ts":1644440400,
"month":2,
"week":6,
"sub1":"sm",
"sub2":"d0q1",
"sub3":"mondata_check",
"sub4":"",
"cost":3745.6,
"sms_count":1362,
"payout":28000.0,
"net_margin":24254.4
},
{
"date":"2022-02-10",
"date_ts":1644440400,
"month":2,
"week":6,
"sub1":"sm",
"sub2":"d1q1",
"sub3":"mondata_check",
"sub4":"",
"cost":3894.98,
"sms_count":1416,
"payout":4000.0,
"net_margin":105.02
},
{
"date":"2022-02-10",
"date_ts":1644440400,
"month":2,
"week":6,
"sub1":"sm",
"sub2":"d0q1",
"sub3":"kadata_check",
"sub4":"",
"cost":3025.92,
"sms_count":1100,
"payout":0,
"net_margin":-3025.92
},
{
"date":"2022-02-10",
"date_ts":1644440400,
"month":2,
"week":6,
"sub1":"ppp",
"sub2":"",
"sub3":"",
"sub4":"",
"cost":0,
"sms_count":0,
"payout":49400.0,
"net_margin":49400.0
},
{
"date":"2022-02-10",
"date_ts":1644440400,
"month":2,
"week":6,
"sub1":"bot",
"sub2":"offers_list",
"sub3":"",
"sub4":"",
"cost":0,
"sms_count":0,
"payout":4000.0,
"net_margin":4000.0
},
{
"date":"2022-02-10",
"date_ts":1644440400,
"month":2,
"week":6,
"sub1":"sm",
"sub2":"bot",
"sub3":"vitrina",
"sub4":"",
"cost":0,
"sms_count":0,
"payout":225.0,
"net_margin":225.0
},
{
"date":"2022-02-11",
"date_ts":1644526800,
"month":2,
"week":6,
"sub1":"sm",
"sub2":"d0q1",
"sub3":"random",
"sub4":"",
"cost":7817.72,
"sms_count":2842,
"payout":37237.0,
"net_margin":29419.28
},
{
"date":"2022-02-11",
"date_ts":1644526800,
"month":2,
"week":6,
"sub1":"sm",
"sub2":"d1q1",
"sub3":"mondata_check",
"sub4":"",
"cost":3735.98,
"sms_count":1358,
"payout":0,
"net_margin":-3735.98
},
{
"date":"2022-02-11",
"date_ts":1644526800,
"month":2,
"week":6,
"sub1":"sm",
"sub2":"d1q1",
"sub3":"vitrina",
"sub4":"",
"cost":8381.16,
"sms_count":3047,
"payout":9120.0,
"net_margin":738.84
},
{
"date":"2022-02-11",
"date_ts":1644526800,
"month":2,
"week":6,
"sub1":"sm",
"sub2":"d0q1",
"sub3":"webbdata_check",
"sub4":"",
"cost":4255.14,
"sms_count":1547,
"payout":14280.0,
"net_margin":10024.86
},
{
"date":"2022-02-11",
"date_ts":1644526800,
"month":2,
"week":6,
"sub1":"sm",
"sub2":"d0q2",
"sub3":"",
"sub4":"",
"cost":7744.36,
"sms_count":2816,
"payout":19125.0,
"net_margin":11380.64
},
{
"date":"2022-02-11",
"date_ts":1644526800,
"month":2,
"week":6,
"sub1":"sm",
"sub2":"d0q1",
"sub3":"mondata_check",
"sub4":"",
"cost":3349.56,
"sms_count":1218,
"payout":36000.0,
"net_margin":32650.44
},
{
"date":"2022-02-11",
"date_ts":1644526800,
"month":2,
"week":6,
"sub1":"sm",
"sub2":"d0q1",
"sub3":"data_check",
"sub4":"",
"cost":5051.06,
"sms_count":1836,
"payout":15895.0,
"net_margin":10843.94
},
{
"date":"2022-02-11",
"date_ts":1644526800,
"month":2,
"week":6,
"sub1":"sm",
"sub2":"d0q3",
"sub3":"",
"sub4":"",
"cost":7701.2,
"sms_count":2800,
"payout":7140.0,
"net_margin":-561.2
},
{
"date":"2022-02-11",
"date_ts":1644526800,
"month":2,
"week":6,
"sub1":"sm",
"sub2":"d0q1",
"sub3":"kadata_check",
"sub4":"",
"cost":2885.4,
"sms_count":1049,
"payout":0,
"net_margin":-2885.4
},
{
"date":"2022-02-11",
"date_ts":1644526800,
"month":2,
"week":6,
"sub1":"sm",
"sub2":"d1q2",
"sub3":"bounce",
"sub4":"",
"cost":2123.72,
"sms_count":772,
"payout":8268.0,
"net_margin":6144.28
},
{
"date":"2022-02-11",
"date_ts":1644526800,
"month":2,
"week":6,
"sub1":"ppp",
"sub2":"",
"sub3":"",
"sub4":"",
"cost":0,
"sms_count":0,
"payout":45600.0,
"net_margin":45600.0
},
{
"date":"2022-02-11",
"date_ts":1644526800,
"month":2,
"week":6,
"sub1":"sm",
"sub2":"bot",
"sub3":"vitrina",
"sub4":"",
"cost":0,
"sms_count":0,
"payout":3055.0,
"net_margin":3055.0
}
]
}
I want to group data by 2 (or more) parameters (in this case by sub1 and sub2) and calculate the sum for the fields payout and cost and get count for the field sms_count and output only these values within groups. For example…
{
"sm":{
"d0q3":[
{
"date":"2022-02-03",
"cost":8230.84,
"sms_count":2993,
"payout":11900
},
{
"date":"2022-02-03",
"cost":8230.84,
"sms_count":2993,
"payout":11900
}
],
"d1q1":[
{
"date":"2022-02-03",
"cost":4043.29,
"sms_count":1470,
"payout":0
}
]
}
}
I tried the following code, but it outputs all values and does not do counts
const groupAndSumMultipleParams = R.pipe(
R.groupBy(R.prop('sub1')),
R.map(R.groupBy(R.prop('sub2'))),
R.values,
R.map(R.reduce(
R.mergeWith(R.ifElse(R.is(Number), R.add, R.identity)),
{}
))
)