【问题标题】:groupBy based on range of values using RxJS in angulargroupBy 基于角度使用 RxJS 的值范围
【发布时间】:2022-12-06 23:23:33
【问题描述】:

我有一个原始数据数组

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

我想以这种格式使用 RxJS 对这些数据进行分组

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

我无法在范围内分组

【问题讨论】:

  • value 是否有点表示该范围内有一个值,或者它是该范围内值数量的计数器?你试过什么了?你卡在哪儿了?
  • value 指示范围内的 bugid 数量的计数器。顺便说一句,范围是天数
  • 我尝试的是,from(arr).pipe(Rx.groupBy(state),Rx.mergeMap(group$=>{ group$.pipe(Rx.count())}),但这会给我计数状态,不确定我如何映射 0-25 之间的天数

标签: javascript angular rxjs rxjs-pipeable-operators ngx-charts


【解决方案1】:

你不需要 rxjs 。

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

【讨论】:

    猜你喜欢
    • 2014-02-21
    • 1970-01-01
    • 2015-06-15
    • 2016-10-21
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    相关资源
    最近更新 更多