3
votes

I have an object and need to combine it with an observable.

The object looks like this:

obj1

let org1Obj = {"category": "1", "testkey": "testvalue"}

The results with JSON.stringify of the observable looks like this:

obj2

[{"id":"51XXgI1w77VerovThbUV","question":"Sdad"},
 {"id":"FxjDsyLBGqtrBx1EHFyM","question":"exanoke"}]

I then have an observable that fetches data and I'm thinking of using forkJoin to combine the two so I end up with the following result:

{"category": "1", 
 "testkey": "testvalue", 
 "questions": [
   {"name": "test1"},
   {"name": "test"}
 ]
}

How do I make org1Obj into an observable so I can use forkJoin on it and how can I add the second observable to "questions": [result from observable]?

1
Converting a value to an observable that emits a single value? It looks like a misuse of observables. This could be done with simple map. The way how exactly they should be combined remains unclear since the values of the result differ from original objects.Estus Flask
The reason I need to convert it is so that I can use forkJoin and the reason for forkJoin is because I need to wait until all the data is available from the second observable. If I try to just combine them the data won't be available from the second observable before the function is run.amunds
Since static object is already available, resulting data will be available in observable map.Estus Flask

1 Answers

3
votes

This is possible with:

const observable1 = Observable.of(obj1);
return Observable.forkJoin(observable1, observable2).map(([obj1, obj2]) => {
  return Object.assign({}, obj1, { questions: obj2 }); // or so
});

And less complicated and more performant way to do this is to skip forkJoin and map an observable to resulting object:

return observable2.map(obj2 => {
  return Object.assign({}, obj1, { questions: obj2 }); // or so
});