Pat Murray
Pat Murray

Reputation: 4275

Patch objects with RXJS

I believe what I am trying to achieve is similar to an HTTP Patch or deep extend function, but I want to achieve it with rxjs. Say I have the following objects:

const arr1 = {
  obj1: { a: 'dog', b: 'cat', c: 'bird' },
  obj2: { a: 'boy', b: 'girl', c: 'guitar' },
  obj3: { a: '1', b: '2', c: '3' }
}

const arr2 = {
  obj1: { a: 'wolf', b: 'lion', c: 'hawk', z: 'bear' },
  obj2: { c: 'car'}
}

I then want to use rxjs in order to get the following output:

const output = {
  obj1: { a: 'wolf', b: 'lion', c: 'hawk', z: 'bear' },
  obj2: { a: 'boy', b: 'girl', c: 'car' },
  obj3: { a: '1', b: '2', c: '3' }
}

Is there an efficient way to achieve this?

Upvotes: 0

Views: 344

Answers (1)

Tiberiu Popescu
Tiberiu Popescu

Reputation: 4524

RxJS is not for merging Objects, it is An API for asynchronous programming with observable streams

If you want to merge Objects you can use Lodash merge

var object = {
  'a': [{ 'b': 2 }, { 'd': 4 }]
};

var other = {
  'a': [{ 'c': 3 }, { 'e': 5 }]
};

_.merge(object, other);
// => { 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }] }

Or the Object spread operator (not officially supported) https://babeljs.io/docs/plugins/transform-object-rest-spread/

Upvotes: 1

Related Questions