What's the best way to merge consecutive Observables' results?












2















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










share|improve this question























  • 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
















2















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










share|improve this question























  • 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














2












2








2








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










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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



















  • 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












3 Answers
3






active

oldest

votes


















3














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.






share|improve this answer































    1














    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.






    share|improve this answer































      1














      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






      share|improve this answer























        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
        });


        }
        });














        draft saved

        draft discarded


















        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









        3














        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.






        share|improve this answer




























          3














          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.






          share|improve this answer


























            3












            3








            3







            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.






            share|improve this answer













            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 2 at 14:45









            DavyDavy

            2,75141528




            2,75141528

























                1














                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.






                share|improve this answer




























                  1














                  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.






                  share|improve this answer


























                    1












                    1








                    1







                    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.






                    share|improve this answer













                    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.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 2 at 14:18









                    trichetrichetrichetriche

                    29k52861




                    29k52861























                        1














                        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






                        share|improve this answer




























                          1














                          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






                          share|improve this answer


























                            1












                            1








                            1







                            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






                            share|improve this answer













                            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







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 2 at 14:32









                            A.WinnenA.Winnen

                            9021110




                            9021110






























                                draft saved

                                draft discarded




















































                                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.




                                draft saved


                                draft discarded














                                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





















































                                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







                                Popular posts from this blog

                                'app-layout' is not a known element: how to share Component with different Modules

                                android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                                WPF add header to Image with URL pettitions [duplicate]