We start with an array that includes the following data structure:
array = [{
key: '2001',
values: [
{ id : '123a', points: 3, hours: 3 },
{ id : '123a', points: 4, hours: 2 },
{ id : '4444', points: 3, hours: 2 },
{ id : '4444', points: 3, hours: 5 },
]
}, {
key: '2002',
values: [
{ id : '12d3a', points: 5, hours: 2 },
{ id : '12w3a', points: 3, hours: 3 },
]
}]
Using a function consisting of various Ramda functions, we managed to organize the data as follows:
const {map, evolve, pipe, groupBy, prop, pluck, sum, toPairs, zipObj} = R;
const a = map(evolve({
values: pipe(
groupBy(prop('id')),
map(pluck('hours')),
map(sum),
toPairs,
map(zipObj(['id', 'hours']))
)
}))
After this organization, we have gathered all "hours" for each id under each key (2001, 2002..) in the array.
The next step is to calculate and add another value named 'sum' to each object. The formula for calculating 'sum' is (points * hours) / (total sum of hours).
Do you have any ideas on how I can achieve this? I tried to modify the const a function but was unsuccessful. Is there another method or approach that can help me perform this operation using Ramda?
array = [{
key: '2001',
values: [
{ id : '123a', hours: 5, sum: 3.4 },
{ id : '4444', hours: 7, sum: 3 }....