What's the best way to merge consecutive Observables' results?
Let's say I have an Observable which results in an Object of 3 arrays like this :
results = {
type1: [...],
type2: [...],
type3: [...],
};
Is there a rxjs operator which allows me to combine multiple observables and their results in order to have a final result like this :
results1 = {
type1: [1, 2],
type2: ['ab', 'cd'],
type3: ['foo', 'bar'],
};
results2 = {
type1: [3, 4, 5],
type2: ['ef', 'gh', 'ij'],
type3: ['ban', 'jo', 'vi'],
};
final_results = {
type1: [1, 2, 3, 4, 5],
type2: ['ab', 'cd', 'ef', 'gh', 'ij'],
type3: ['foo', 'bar', 'ban', 'jo', 'vi'],
};
?????
For the record I'm using Angular 2+ and typescript, and I want to keep using Observable streams and rxjs operators, but according to their documentation I'm not sure they're one which could do something like 'deep merging ??'
Thanks for helping
angular rxjs concatenation
add a comment |
Let's say I have an Observable which results in an Object of 3 arrays like this :
results = {
type1: [...],
type2: [...],
type3: [...],
};
Is there a rxjs operator which allows me to combine multiple observables and their results in order to have a final result like this :
results1 = {
type1: [1, 2],
type2: ['ab', 'cd'],
type3: ['foo', 'bar'],
};
results2 = {
type1: [3, 4, 5],
type2: ['ef', 'gh', 'ij'],
type3: ['ban', 'jo', 'vi'],
};
final_results = {
type1: [1, 2, 3, 4, 5],
type2: ['ab', 'cd', 'ef', 'gh', 'ij'],
type3: ['foo', 'bar', 'ban', 'jo', 'vi'],
};
?????
For the record I'm using Angular 2+ and typescript, and I want to keep using Observable streams and rxjs operators, but according to their documentation I'm not sure they're one which could do something like 'deep merging ??'
Thanks for helping
angular rxjs concatenation
There are multiple operators which can merge the data of two observables, but concatenating the values is something that is logic-specific. You can use one of the operators to merge data and concatenate it manually.
– Sachin Gupta
Jan 2 at 14:12
add a comment |
Let's say I have an Observable which results in an Object of 3 arrays like this :
results = {
type1: [...],
type2: [...],
type3: [...],
};
Is there a rxjs operator which allows me to combine multiple observables and their results in order to have a final result like this :
results1 = {
type1: [1, 2],
type2: ['ab', 'cd'],
type3: ['foo', 'bar'],
};
results2 = {
type1: [3, 4, 5],
type2: ['ef', 'gh', 'ij'],
type3: ['ban', 'jo', 'vi'],
};
final_results = {
type1: [1, 2, 3, 4, 5],
type2: ['ab', 'cd', 'ef', 'gh', 'ij'],
type3: ['foo', 'bar', 'ban', 'jo', 'vi'],
};
?????
For the record I'm using Angular 2+ and typescript, and I want to keep using Observable streams and rxjs operators, but according to their documentation I'm not sure they're one which could do something like 'deep merging ??'
Thanks for helping
angular rxjs concatenation
Let's say I have an Observable which results in an Object of 3 arrays like this :
results = {
type1: [...],
type2: [...],
type3: [...],
};
Is there a rxjs operator which allows me to combine multiple observables and their results in order to have a final result like this :
results1 = {
type1: [1, 2],
type2: ['ab', 'cd'],
type3: ['foo', 'bar'],
};
results2 = {
type1: [3, 4, 5],
type2: ['ef', 'gh', 'ij'],
type3: ['ban', 'jo', 'vi'],
};
final_results = {
type1: [1, 2, 3, 4, 5],
type2: ['ab', 'cd', 'ef', 'gh', 'ij'],
type3: ['foo', 'bar', 'ban', 'jo', 'vi'],
};
?????
For the record I'm using Angular 2+ and typescript, and I want to keep using Observable streams and rxjs operators, but according to their documentation I'm not sure they're one which could do something like 'deep merging ??'
Thanks for helping
angular rxjs concatenation
angular rxjs concatenation
asked Jan 2 at 14:09
LaMutLaMut
377
377
There are multiple operators which can merge the data of two observables, but concatenating the values is something that is logic-specific. You can use one of the operators to merge data and concatenate it manually.
– Sachin Gupta
Jan 2 at 14:12
add a comment |
There are multiple operators which can merge the data of two observables, but concatenating the values is something that is logic-specific. You can use one of the operators to merge data and concatenate it manually.
– Sachin Gupta
Jan 2 at 14:12
There are multiple operators which can merge the data of two observables, but concatenating the values is something that is logic-specific. You can use one of the operators to merge data and concatenate it manually.
– Sachin Gupta
Jan 2 at 14:12
There are multiple operators which can merge the data of two observables, but concatenating the values is something that is logic-specific. You can use one of the operators to merge data and concatenate it manually.
– Sachin Gupta
Jan 2 at 14:12
add a comment |
3 Answers
3
active
oldest
votes
You are looking for the scan operator:
https://www.learnrxjs.io/operators/transformation/scan.html
It is similar to the reduce operator, but the reduce operator will only emit at the end of the stream. The scan operator will emit the intermediate value every time.
add a comment |
This isn't observable-related, this is plain old Javascript.
The only operator you have to know is combineLatest
:
combineLatest(results1$, results2$).pipe(
map((results1, results2) => {
const ret = {};
Object.keys(results1).foreEach(key => ret[key] = results1[key].concat(results2[key]));
return ret;
})
).subscribe(res => console.log(res)); // should display your expected result
Don't be mistaken : RxJS operators manipulate the stream (the Observable itself), not the data in it.
If you want to handle the internal data, this becomes Javascript.
add a comment |
you can use the reduce operator to aggregate a single result from many emitted values.
It's up to you to merge the single arrays since this is not related to rxjs
Simple example: https://stackblitz.com/edit/angular-cffyyh
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54007829%2fwhats-the-best-way-to-merge-consecutive-observables-results%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are looking for the scan operator:
https://www.learnrxjs.io/operators/transformation/scan.html
It is similar to the reduce operator, but the reduce operator will only emit at the end of the stream. The scan operator will emit the intermediate value every time.
add a comment |
You are looking for the scan operator:
https://www.learnrxjs.io/operators/transformation/scan.html
It is similar to the reduce operator, but the reduce operator will only emit at the end of the stream. The scan operator will emit the intermediate value every time.
add a comment |
You are looking for the scan operator:
https://www.learnrxjs.io/operators/transformation/scan.html
It is similar to the reduce operator, but the reduce operator will only emit at the end of the stream. The scan operator will emit the intermediate value every time.
You are looking for the scan operator:
https://www.learnrxjs.io/operators/transformation/scan.html
It is similar to the reduce operator, but the reduce operator will only emit at the end of the stream. The scan operator will emit the intermediate value every time.
answered Jan 2 at 14:45
DavyDavy
2,75141528
2,75141528
add a comment |
add a comment |
This isn't observable-related, this is plain old Javascript.
The only operator you have to know is combineLatest
:
combineLatest(results1$, results2$).pipe(
map((results1, results2) => {
const ret = {};
Object.keys(results1).foreEach(key => ret[key] = results1[key].concat(results2[key]));
return ret;
})
).subscribe(res => console.log(res)); // should display your expected result
Don't be mistaken : RxJS operators manipulate the stream (the Observable itself), not the data in it.
If you want to handle the internal data, this becomes Javascript.
add a comment |
This isn't observable-related, this is plain old Javascript.
The only operator you have to know is combineLatest
:
combineLatest(results1$, results2$).pipe(
map((results1, results2) => {
const ret = {};
Object.keys(results1).foreEach(key => ret[key] = results1[key].concat(results2[key]));
return ret;
})
).subscribe(res => console.log(res)); // should display your expected result
Don't be mistaken : RxJS operators manipulate the stream (the Observable itself), not the data in it.
If you want to handle the internal data, this becomes Javascript.
add a comment |
This isn't observable-related, this is plain old Javascript.
The only operator you have to know is combineLatest
:
combineLatest(results1$, results2$).pipe(
map((results1, results2) => {
const ret = {};
Object.keys(results1).foreEach(key => ret[key] = results1[key].concat(results2[key]));
return ret;
})
).subscribe(res => console.log(res)); // should display your expected result
Don't be mistaken : RxJS operators manipulate the stream (the Observable itself), not the data in it.
If you want to handle the internal data, this becomes Javascript.
This isn't observable-related, this is plain old Javascript.
The only operator you have to know is combineLatest
:
combineLatest(results1$, results2$).pipe(
map((results1, results2) => {
const ret = {};
Object.keys(results1).foreEach(key => ret[key] = results1[key].concat(results2[key]));
return ret;
})
).subscribe(res => console.log(res)); // should display your expected result
Don't be mistaken : RxJS operators manipulate the stream (the Observable itself), not the data in it.
If you want to handle the internal data, this becomes Javascript.
answered Jan 2 at 14:18
trichetrichetrichetriche
29k52861
29k52861
add a comment |
add a comment |
you can use the reduce operator to aggregate a single result from many emitted values.
It's up to you to merge the single arrays since this is not related to rxjs
Simple example: https://stackblitz.com/edit/angular-cffyyh
add a comment |
you can use the reduce operator to aggregate a single result from many emitted values.
It's up to you to merge the single arrays since this is not related to rxjs
Simple example: https://stackblitz.com/edit/angular-cffyyh
add a comment |
you can use the reduce operator to aggregate a single result from many emitted values.
It's up to you to merge the single arrays since this is not related to rxjs
Simple example: https://stackblitz.com/edit/angular-cffyyh
you can use the reduce operator to aggregate a single result from many emitted values.
It's up to you to merge the single arrays since this is not related to rxjs
Simple example: https://stackblitz.com/edit/angular-cffyyh
answered Jan 2 at 14:32
A.WinnenA.Winnen
9021110
9021110
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54007829%2fwhats-the-best-way-to-merge-consecutive-observables-results%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
There are multiple operators which can merge the data of two observables, but concatenating the values is something that is logic-specific. You can use one of the operators to merge data and concatenate it manually.
– Sachin Gupta
Jan 2 at 14:12