0

I want to use Rxjs map() method to switch values between two json objects. Or if there are any better rxjs methods I am open to suggestions

For example if I have two json objects:

[{"firstname":"abc","lastname":"bcd"},{"firstname":"xyz","lastname":"zzz"}]

How do I for example switch the values of first name from both json objects in the array as well as the lastnames? I am very new to rxjs!

Thanks!

Brhaka
  • 1,622
  • 3
  • 11
  • 31
Kits Gum
  • 1
  • 1

1 Answers1

0

In your example, swapping the entire objects will work:

let swapped = observable.pipe(map(([first, second]) => [second, first]));

In older versions of JS (and made a bit verbose on purpose):

function swapPair(arr) {
    var first = arr[0];
    var second = arr[1];
    return [second, first];
}

var swapped = observable.pipe(map(swapPair));

If you want a more generic way to swap properties -- I was about to write something, but really it's been done before:

hugo
  • 3,067
  • 2
  • 12
  • 22