In a nested object of arrays , the percentage value should be mapped to an object which has greater count . The data object has two arrays "distinctCount" and "groupByAndCount" . The distinctCount contains an object .
{
"key": "total",
"value": 7
}
The "groupByAndCount" based on the key name the objects should be grouped and the value with highest count should be fetched and divided by value obtained in "distinctCount" and multiplied by 100 to get the percentage Example: In key "marital_status" the "Single" value has the highest count "6" compared to the value "Married" which has low count "1"
{
"key": "marital_status",
"value": "Single",
"count": 6/7*100 = 85.7%
}
I am a beginner in reduce function , I think reducer function is required in getting the reduced object with percentage
const result = data.groupByAndCount.map(e => e.reduce((a, { key, value, count }) =>
(a['key'] = key,a['value'] = value,a['count'] = count, a), {}));
const data = {
distinctCount : [
{
"key": "total",
"value": 7
},
{
"key": "member_total",
"value": 7
}
]
,
groupByAndCount : [
[
{
"key": "marital_status",
"value": "Married",
"count": 1
},
{
"key": "marital_status",
"value": "Single",
"count": 6
}
],
[
{
"key": "payment",
"value": "POST",
"count": 7
}
],
[
{
"key": "customer_code",
"value": "ABC",
"count": 1
},
{
"key": "customer_code",
"value": "DEF",
"count": 6
}
]
]
};
The expected result :
const result = {
distinctCount : [
{
"key": "total",
"value": 7
},
{
"key": "member_total",
"value": 7
}
]
,
groupByAndCount : [
[
{
"key": "marital_status",
"value": "Single",
"count": 85.71
}
],
[
{
"key": "payment",
"value": "POST",
"count": 100
}
],
[
{
"key": "customer_code",
"value": "DEF",
"count": 85.71
}
]
]
};
distinctCountobject belongs to whichgroupByAndCountobject? As both have different key values and different count itself.