Catalin Vasilescu
Catalin Vasilescu

Reputation: 41

Adding a new value to an object in javascript

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 }....

Upvotes: 0

Views: 71

Answers (2)

Scott Sauyet
Scott Sauyet

Reputation: 50807

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>

Upvotes: 1

Nikhil Aggarwal
Nikhil Aggarwal

Reputation: 28475

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;
});

Upvotes: 0

Related Questions