【问题标题】:How transform values (true or false to Yes or No) in Array with objects with RXJS?如何使用带有 RXJS 的对象将 Array 中的值(true 或 false 转换为 Yes 或 No)?
【发布时间】:2018-05-02 06:20:24
【问题描述】:

我从 Firebase 收到以下(示例)数据:

[
  {
    'customer': 'John',
    'active': true
  },
  {
    'customer': 'Doe',
    'active': true
  },
  {
    'customer': 'Frenz',
    'active': false
  }
];

在我的表格中,我想将值显示为“是”或“否”。 我正在使用 Angular,所以我想在订阅 te observable 时更改这些值。

到目前为止,我认为我必须使用地图运算符,例如:Transforming Observable with .map

但是这个例子增加了一个值,我想改变每个对象中的每个值。

【问题讨论】:

    标签: javascript angular rxjs


    【解决方案1】:

    您可以使用map(我假设您使用的是 RxJS 5.5):

    source
      .pipe(
        map(array => array.map(obj => {
          obj.active = obj.active ? 'Yes' : 'No';
          return obj;
        })),
      )
    

    我知道使用两个 maps 看起来很奇怪,但外部的一个来自 RxJS,而内部的只是 Array.map 更新每个对象。

    【讨论】:

    • 还要确保像这样导入 RxJS 可管道映射运算符: import {map} from 'rxjs/operators';
    猜你喜欢
    • 2018-04-08
    • 2011-01-31
    • 1970-01-01
    • 2011-04-09
    • 1970-01-01
    • 2011-01-18
    • 2013-09-09
    • 1970-01-01
    • 2011-12-16
    相关资源
    最近更新 更多