简体   繁体   中英

How transform values (true or false to Yes or No) in Array with objects with RXJS?

I receive the following (example) data from Firebase:

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

In my table I want to show the value as 'Yes' or 'No'. I'm using Angular so I want to change the values when I subscribe to te observerable.

So far I think I have to use the map-operator like: Transforming Observable with .map

But this example adds a value, I want to change every value in every object.

You can use map (I'm assuming you're using RxJS 5.5):

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

I know it looks weird to use two map s but the outer one is from RxJS and the inner one is just Array.map that updates each object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM