0

I have the following array

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 },
]
}]

With this function

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']))
        )
    }))

I gathered all "hours" for each id for each key (2001, 2002..)

array = [{
 key: '2001',
 values: [
  { id : '123a', hours: 5 },
  { id : '4444',  hours: 7 }....

Now I want to add another value to each object, called sum. var sum = (points*hours)/(sum(hours)).

Any idea how can i do this? I've tried to change const a but is not working and I don't know if ramda can to this operation. Maybe an alternative?

array = [{
 key: '2001',
 values: [
  { id : '123a', hours: 5, sum: 3.4 },
  { id : '4444',  hours: 7, sum: 3 }....
3
  • What is sum(hours) in the formula? Commented May 15, 2018 at 12:34
  • @NikhilAggarwal the sum of all hours...for example, for id 123a i have 5 and for id 4444 i have 7 Commented May 15, 2018 at 12:38
  • @NikhilAggarwal something like this: for id 123 a sum= (3*3+4*2)/5 Commented May 15, 2018 at 12:40

2 Answers 2

1

Here is a Ramda version that works by reducing to an intermediate structure with id, total hours, and total points, then converting to your final format:

const {map, evolve, pipe, groupBy, prop, reduce, values} = R;

const array = [{"key": "2001", "values": [{"hours": 3, "id": "123a", "points": 3}, {"hours": 2, "id": "123a", "points": 4}, {"hours": 2, "id": "4444", "points": 3}, {"hours": 5, "id": "4444", "points": 3}]}, {"key": "2002", "values": [{"hours": 2, "id": "12d3a", "points": 5}, {"hours": 3, "id": "12w3a", "points": 3}]}]

const a = map(evolve({
  values: pipe(
    groupBy(prop('id')),
    map(reduce(
      ({tp, th}, {id, points, hours}) => ({id, tp: tp + points * hours, th: th + hours})
      , {tp: 0, th: 0}
    )),
    values,
    map(({id, th, tp}) => ({id, hours: th, sum: tp/th})),
  )
}))

console.log(a(array))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

0

Try following

var 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 },
]
}];

array.forEach((item) => {
  var map = {};
  item.values.forEach((obj) => {
    map[obj.id] = map[obj.id] || [];
    map[obj.id].push(obj);
  });
  var values = [];
  Object.values(map).forEach((vals) => {
    var n = 0, d = 0;
    vals.forEach((val) => {
      n += val.points * val.hours;
      d += val.hours;
    });
    values.push({
      'id' : vals[0].id,
      'hours' : d,
      'sum' : n/d
    });
  });
  item.values = values;
});


console.log(array);

You can also try following logic

array.forEach((item) => {
  var map = {};
  item.values.forEach((obj) => {
    map[obj.id] = map[obj.id] || {id : obj.id, n: 0, hours: 0};
    map[obj.id].hours += obj.hours;
    map[obj.id].n += (obj.points * obj.hours);
  });
  var values = Object.values(map);
  values.forEach((val) => {
    val.sum = val.n / val.hours;
    delete val.n;
  });
  item.values = values;
});

3 Comments

@CatalinVasilescu - I have added another way. Check if it helps!
It helps. Thanks!
I had tried and it is working fine for me. Can you please recheck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.