0

I have a raw data array

[
  {bugid: b1 , state: 'foo', days: 2}, 
  {bugid: b2, state: 'bar', days: 41}, 
  {bugid: b3, state: 'foo', days: 45}
]

I want to group this data using RxJS in this format

{
  '0-25': [{ name: foo, value: 1}, {name: bar, value: 0}], 
  '26-50': [{name: foo, value: 1}, {name: bar, value: 1}]
}

I am not able to group in the range

D M
  • 5,769
  • 4
  • 12
  • 27
  • 2
    Is `value` a bit indicating that there is a value in that range, or is it a counter of the number of values in that range? What have you tried so far? Where are you getting stuck? – D M Dec 05 '22 at 15:13
  • value is indicating the counter for number of bugid that are in the range. Btw range is the no of days – confused_dude_13 Dec 05 '22 at 16:51
  • What I tried was, from(arr).pipe(Rx.groupBy(state),Rx.mergeMap(group$=>{ group$.pipe(Rx.count())}), but this will give me the count of state, not sure how do i map the days between 0-25 – confused_dude_13 Dec 05 '22 at 16:53

1 Answers1

0

You don't need rxjs for this.

const data = [
  { state: 'foo', days: 2 }, 
  { state: 'bar', days: 41 }, 
  { state: 'foo', days: 45 }
];

// get all possible states
const states = [...new Set(data.map(item => item.state))];

const result = {
  '0-25': getCounts(0, 25),
  '26-60': getCounts(26, 50)
};

function getCounts(from, to) {
  return states.map(state => ({ name: state, value: getValue(state, from, to) }));
}

function getValue(state, from, to) {
  return data.filter(item => item.state === state && item.days >= from && item.days <= to).length;
}
Tortila
  • 158
  • 8