Angular RxJs level up during looping an arrayAngular Subscribe to Observable after URL requestWith rxjs Observable<Array<T>, how can I make an http service call for each array item and assign the result of that observable to a property?What is the correct way to share the result of an Angular Http network call in RxJs 5?Angular/RxJs When should I unsubscribe from `Subscription`RxJS zip not working while forkJoin doesAngular 4 RxJS polling with switchMapRXJS - Processing an array of itemsRxJS 5.5.0-beta : mergeMap behavior differing from map + mergeAllangular rxjs: access parameter on http error inside pipe/concatMap?Angular 6 multiple HTTP request RxJsCatching an error and returning of(undefined) stops the stream (Angular 7, rxjs6)updating a list of objects within an observable using RxJS 6 and Angular 6

Why would anyone even use a Portkey?

How to describe POV characters?

Why did the Apple //e make a hideous noise if you inserted the disk upside down?

How can I deal with extreme temperatures in a hotel room?

Ways to get SMD resistors from a strip

Is ALTER TABLE ... DROP COLUMN really a metadata only operation?

If you kill a Solar Angel can you use its Slaying Longbow?

13th chords on guitar

pgfmath does not work

Having to constantly redo everything because I don't know how to do it?

Is it okay to fade a human face just to create some space to place important content over it?

Two palindromes are not enough

Fully submerged water bath for stove top baking?

if a USA citizen marries a foreign citizen who has kid from previous marriage

Early 2000s movie about time travel, protagonist travels back to save girlfriend, then into multiple points in future

Security Patch SUPEE-11155 - Possible issues?

Possessive adjective "Juan y Antonio viven con su hermano."

Journal standards vs. personal standards

Alien life interbreeding with Earth life

Could you fall off a planet if it was being accelerated by engines?

How can I open this door latch with the knobs removed?

Discworld quote about an "old couple" who having said everything to each other, can finally go about living their lives

What verb for taking advantage fits in "I don't want to ________ on the friendship"?

Bin Packing with Relational Penalization



Angular RxJs level up during looping an array


Angular Subscribe to Observable after URL requestWith rxjs Observable<Array<T>, how can I make an http service call for each array item and assign the result of that observable to a property?What is the correct way to share the result of an Angular Http network call in RxJs 5?Angular/RxJs When should I unsubscribe from `Subscription`RxJS zip not working while forkJoin doesAngular 4 RxJS polling with switchMapRXJS - Processing an array of itemsRxJS 5.5.0-beta : mergeMap behavior differing from map + mergeAllangular rxjs: access parameter on http error inside pipe/concatMap?Angular 6 multiple HTTP request RxJsCatching an error and returning of(undefined) stops the stream (Angular 7, rxjs6)updating a list of objects within an observable using RxJS 6 and Angular 6






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I have an http get request to get an array like



[
name: 'name1', id: 1, specialProp: [] ,
name: 'name2', id: 2, specialProp: []
]


I need to get each of array items, take an id and send a request to server to get some information. The results should be written into the property specialProp. After that I need to take the array of the prop specialProp and for each item get some data, put it into anotherSpecialProp. In the end I should have the final array like



[
name: 'name1', id: 1, specialProp: [
name: 'c', anotherSpecialProp: [],
name: 'd', anotherSpecialProp: []
],

name: 'name2', id: 2, specialProp: [
name: 'a', anotherSpecialProp: [],
name: 'b', anotherSpecialProp: []
]
]


I have the code:



this.http.get(url)
.pipe(
switchMap((mainItemArr: any) => from(mainItemArr)),
mergeMap((mainItem: any): any =>
return this.getSomeInfo(mainItem.Id) //another http get request
.pipe(
map((data: any): any =>
return Object.assign(mainItem, specialProp: data )
),
switchMap((mainItemArr: any): any => from(mainItemArr.specialProp)),
concatMap((item: any): any =>
return this.getSomeOtherInfo(item.Id) // one more http get request
.pipe(
map((data: any): any => Object.assign(, task, anotherSpecialProp: data ))
)
),
)
)
)


So in subscribe I receive just the items, not the whole mainItemArr.
Could anyone please assist me with the issue?:)










share|improve this question
























  • please, put your code between ``` lang-js and ``` for better readability. See "formatting help" for details

    – kos
    Mar 25 at 15:39











  • This may help: stackoverflow.com/a/53109081/9386929

    – nircraft
    Mar 25 at 17:12











  • Possible duplicate of Angular Subscribe to Observable after URL request

    – nircraft
    Mar 25 at 17:13











  • @nircraft, I have checked the articles, but I can not get your point, how the article could help me(((( Perhaps, you could provide me with more details of your answer? I receive the new data, but I can not get the initial array, as the stream gives me the last stream elements.

    – Vika
    Mar 25 at 18:31

















1















I have an http get request to get an array like



[
name: 'name1', id: 1, specialProp: [] ,
name: 'name2', id: 2, specialProp: []
]


I need to get each of array items, take an id and send a request to server to get some information. The results should be written into the property specialProp. After that I need to take the array of the prop specialProp and for each item get some data, put it into anotherSpecialProp. In the end I should have the final array like



[
name: 'name1', id: 1, specialProp: [
name: 'c', anotherSpecialProp: [],
name: 'd', anotherSpecialProp: []
],

name: 'name2', id: 2, specialProp: [
name: 'a', anotherSpecialProp: [],
name: 'b', anotherSpecialProp: []
]
]


I have the code:



this.http.get(url)
.pipe(
switchMap((mainItemArr: any) => from(mainItemArr)),
mergeMap((mainItem: any): any =>
return this.getSomeInfo(mainItem.Id) //another http get request
.pipe(
map((data: any): any =>
return Object.assign(mainItem, specialProp: data )
),
switchMap((mainItemArr: any): any => from(mainItemArr.specialProp)),
concatMap((item: any): any =>
return this.getSomeOtherInfo(item.Id) // one more http get request
.pipe(
map((data: any): any => Object.assign(, task, anotherSpecialProp: data ))
)
),
)
)
)


So in subscribe I receive just the items, not the whole mainItemArr.
Could anyone please assist me with the issue?:)










share|improve this question
























  • please, put your code between ``` lang-js and ``` for better readability. See "formatting help" for details

    – kos
    Mar 25 at 15:39











  • This may help: stackoverflow.com/a/53109081/9386929

    – nircraft
    Mar 25 at 17:12











  • Possible duplicate of Angular Subscribe to Observable after URL request

    – nircraft
    Mar 25 at 17:13











  • @nircraft, I have checked the articles, but I can not get your point, how the article could help me(((( Perhaps, you could provide me with more details of your answer? I receive the new data, but I can not get the initial array, as the stream gives me the last stream elements.

    – Vika
    Mar 25 at 18:31













1












1








1








I have an http get request to get an array like



[
name: 'name1', id: 1, specialProp: [] ,
name: 'name2', id: 2, specialProp: []
]


I need to get each of array items, take an id and send a request to server to get some information. The results should be written into the property specialProp. After that I need to take the array of the prop specialProp and for each item get some data, put it into anotherSpecialProp. In the end I should have the final array like



[
name: 'name1', id: 1, specialProp: [
name: 'c', anotherSpecialProp: [],
name: 'd', anotherSpecialProp: []
],

name: 'name2', id: 2, specialProp: [
name: 'a', anotherSpecialProp: [],
name: 'b', anotherSpecialProp: []
]
]


I have the code:



this.http.get(url)
.pipe(
switchMap((mainItemArr: any) => from(mainItemArr)),
mergeMap((mainItem: any): any =>
return this.getSomeInfo(mainItem.Id) //another http get request
.pipe(
map((data: any): any =>
return Object.assign(mainItem, specialProp: data )
),
switchMap((mainItemArr: any): any => from(mainItemArr.specialProp)),
concatMap((item: any): any =>
return this.getSomeOtherInfo(item.Id) // one more http get request
.pipe(
map((data: any): any => Object.assign(, task, anotherSpecialProp: data ))
)
),
)
)
)


So in subscribe I receive just the items, not the whole mainItemArr.
Could anyone please assist me with the issue?:)










share|improve this question
















I have an http get request to get an array like



[
name: 'name1', id: 1, specialProp: [] ,
name: 'name2', id: 2, specialProp: []
]


I need to get each of array items, take an id and send a request to server to get some information. The results should be written into the property specialProp. After that I need to take the array of the prop specialProp and for each item get some data, put it into anotherSpecialProp. In the end I should have the final array like



[
name: 'name1', id: 1, specialProp: [
name: 'c', anotherSpecialProp: [],
name: 'd', anotherSpecialProp: []
],

name: 'name2', id: 2, specialProp: [
name: 'a', anotherSpecialProp: [],
name: 'b', anotherSpecialProp: []
]
]


I have the code:



this.http.get(url)
.pipe(
switchMap((mainItemArr: any) => from(mainItemArr)),
mergeMap((mainItem: any): any =>
return this.getSomeInfo(mainItem.Id) //another http get request
.pipe(
map((data: any): any =>
return Object.assign(mainItem, specialProp: data )
),
switchMap((mainItemArr: any): any => from(mainItemArr.specialProp)),
concatMap((item: any): any =>
return this.getSomeOtherInfo(item.Id) // one more http get request
.pipe(
map((data: any): any => Object.assign(, task, anotherSpecialProp: data ))
)
),
)
)
)


So in subscribe I receive just the items, not the whole mainItemArr.
Could anyone please assist me with the issue?:)







angular http rxjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 26 at 13:15









kos

2,0911 gold badge5 silver badges17 bronze badges




2,0911 gold badge5 silver badges17 bronze badges










asked Mar 25 at 14:53









VikaVika

153 bronze badges




153 bronze badges












  • please, put your code between ``` lang-js and ``` for better readability. See "formatting help" for details

    – kos
    Mar 25 at 15:39











  • This may help: stackoverflow.com/a/53109081/9386929

    – nircraft
    Mar 25 at 17:12











  • Possible duplicate of Angular Subscribe to Observable after URL request

    – nircraft
    Mar 25 at 17:13











  • @nircraft, I have checked the articles, but I can not get your point, how the article could help me(((( Perhaps, you could provide me with more details of your answer? I receive the new data, but I can not get the initial array, as the stream gives me the last stream elements.

    – Vika
    Mar 25 at 18:31

















  • please, put your code between ``` lang-js and ``` for better readability. See "formatting help" for details

    – kos
    Mar 25 at 15:39











  • This may help: stackoverflow.com/a/53109081/9386929

    – nircraft
    Mar 25 at 17:12











  • Possible duplicate of Angular Subscribe to Observable after URL request

    – nircraft
    Mar 25 at 17:13











  • @nircraft, I have checked the articles, but I can not get your point, how the article could help me(((( Perhaps, you could provide me with more details of your answer? I receive the new data, but I can not get the initial array, as the stream gives me the last stream elements.

    – Vika
    Mar 25 at 18:31
















please, put your code between ``` lang-js and ``` for better readability. See "formatting help" for details

– kos
Mar 25 at 15:39





please, put your code between ``` lang-js and ``` for better readability. See "formatting help" for details

– kos
Mar 25 at 15:39













This may help: stackoverflow.com/a/53109081/9386929

– nircraft
Mar 25 at 17:12





This may help: stackoverflow.com/a/53109081/9386929

– nircraft
Mar 25 at 17:12













Possible duplicate of Angular Subscribe to Observable after URL request

– nircraft
Mar 25 at 17:13





Possible duplicate of Angular Subscribe to Observable after URL request

– nircraft
Mar 25 at 17:13













@nircraft, I have checked the articles, but I can not get your point, how the article could help me(((( Perhaps, you could provide me with more details of your answer? I receive the new data, but I can not get the initial array, as the stream gives me the last stream elements.

– Vika
Mar 25 at 18:31





@nircraft, I have checked the articles, but I can not get your point, how the article could help me(((( Perhaps, you could provide me with more details of your answer? I receive the new data, but I can not get the initial array, as the stream gives me the last stream elements.

– Vika
Mar 25 at 18:31












2 Answers
2






active

oldest

votes


















2














The main trick is to use map to merge scoped property with request result.



Here's a rough example how to achieve this for the first level (specialProp):



this.http.get(url).pipe(
mergeMap(mainItemArr =>
// forkJoin will wait for each request to complete
return forkJoin(
// make a subsequent request for each item in mainItemArr
mainItemArr.map(mainItem =>
return this.getSomeInfo(mainItem.Id).pipe(
// merge getSomeInfo result with the mainItem
map(someInfo =>
return
...mainItem,
specialProp: someInfo
;
)
)
)
)
)
)


For the anotherSpecialProp requests — you'll need to go one level deeper.



In a real world application I'd suggest splitting those subsequent calls into separate functions/methods.



NOTE:



You don't need turning array into Observable:



mergeMap(mainArray => mainArray)


Instead you might keep it in the JS scope and do subsequent requests in the mergeMap, e.g.:



mergeMap(mainArray => 
// making sub requests here
)


Using mergeMap to turn array into Observable should work too, though it might be more confusing when diving 1 level deeper, imho. Anyway, the map does the main trick.



Hope this helps






share|improve this answer
































    3














    If I get it right, what you need to do is the following:



    • fetch the initial array from back end

    • for each element of the array, call getSomeInfo and store the result, which should be an array, into the specialProp property

    • then, for each entry in the specialProp array, you want to call the getSomeOtherInfo method, fetch some more data and store it into a property called anotherSpecialProp

    If all this is true, then you can try something along these lines



    getArray()
    .pipe(
    mergeMap(mainArray => mainArray), // unwind the array received
    switchMap(mainItem => getSomeInfo(mainItem.id) // fetch the first set of info from backend
    .pipe(
    tap(someInfo =>
    mainItem['specialProp'] = someInfo; // wrote someInfo into specialProp property
    ),
    mergeMap(specialProps => specialProps), // unwind the array of specialProps
    switchMap(specialProp => getSomeOtherInfo(specialProp.name) // for each specialProp fetch the additional data
    .pipe(
    tap(someOtherInfo =>
    specialProp['anotherSpecialProp'] = someOtherInfo // store additional data into anotherSpecialProp property
    )
    )
    ),
    toArray(), // rewind the array of specialProps and return it
    map(() => mainItem)
    )
    ),
    toArray() // rewind the array of mainItems and return it
    )


    The thing you may want to notice is the use of mergeMap with an Array, e.g. mergeMap(mainArray => mainArray).



    mergeMap accepts as input a function which returns an ObservableInput. An Array is an ObservableInput which emits all of its items synchronously before completing. So, passing a function which returns an Array to mergeMap means to emit all the elements of the Array.



    You can find an example of the above example here






    share|improve this answer























    • Thank you very much, guys! Your answers were very useful! It allowed me to get some understanding RxJs as I am a newbie in RxJs and a step to dive deeper into this library! thank you!

      – Vika
      Mar 28 at 8:07













    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%2f55340585%2fangular-rxjs-level-up-during-looping-an-array%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    The main trick is to use map to merge scoped property with request result.



    Here's a rough example how to achieve this for the first level (specialProp):



    this.http.get(url).pipe(
    mergeMap(mainItemArr =>
    // forkJoin will wait for each request to complete
    return forkJoin(
    // make a subsequent request for each item in mainItemArr
    mainItemArr.map(mainItem =>
    return this.getSomeInfo(mainItem.Id).pipe(
    // merge getSomeInfo result with the mainItem
    map(someInfo =>
    return
    ...mainItem,
    specialProp: someInfo
    ;
    )
    )
    )
    )
    )
    )


    For the anotherSpecialProp requests — you'll need to go one level deeper.



    In a real world application I'd suggest splitting those subsequent calls into separate functions/methods.



    NOTE:



    You don't need turning array into Observable:



    mergeMap(mainArray => mainArray)


    Instead you might keep it in the JS scope and do subsequent requests in the mergeMap, e.g.:



    mergeMap(mainArray => 
    // making sub requests here
    )


    Using mergeMap to turn array into Observable should work too, though it might be more confusing when diving 1 level deeper, imho. Anyway, the map does the main trick.



    Hope this helps






    share|improve this answer





























      2














      The main trick is to use map to merge scoped property with request result.



      Here's a rough example how to achieve this for the first level (specialProp):



      this.http.get(url).pipe(
      mergeMap(mainItemArr =>
      // forkJoin will wait for each request to complete
      return forkJoin(
      // make a subsequent request for each item in mainItemArr
      mainItemArr.map(mainItem =>
      return this.getSomeInfo(mainItem.Id).pipe(
      // merge getSomeInfo result with the mainItem
      map(someInfo =>
      return
      ...mainItem,
      specialProp: someInfo
      ;
      )
      )
      )
      )
      )
      )


      For the anotherSpecialProp requests — you'll need to go one level deeper.



      In a real world application I'd suggest splitting those subsequent calls into separate functions/methods.



      NOTE:



      You don't need turning array into Observable:



      mergeMap(mainArray => mainArray)


      Instead you might keep it in the JS scope and do subsequent requests in the mergeMap, e.g.:



      mergeMap(mainArray => 
      // making sub requests here
      )


      Using mergeMap to turn array into Observable should work too, though it might be more confusing when diving 1 level deeper, imho. Anyway, the map does the main trick.



      Hope this helps






      share|improve this answer



























        2












        2








        2







        The main trick is to use map to merge scoped property with request result.



        Here's a rough example how to achieve this for the first level (specialProp):



        this.http.get(url).pipe(
        mergeMap(mainItemArr =>
        // forkJoin will wait for each request to complete
        return forkJoin(
        // make a subsequent request for each item in mainItemArr
        mainItemArr.map(mainItem =>
        return this.getSomeInfo(mainItem.Id).pipe(
        // merge getSomeInfo result with the mainItem
        map(someInfo =>
        return
        ...mainItem,
        specialProp: someInfo
        ;
        )
        )
        )
        )
        )
        )


        For the anotherSpecialProp requests — you'll need to go one level deeper.



        In a real world application I'd suggest splitting those subsequent calls into separate functions/methods.



        NOTE:



        You don't need turning array into Observable:



        mergeMap(mainArray => mainArray)


        Instead you might keep it in the JS scope and do subsequent requests in the mergeMap, e.g.:



        mergeMap(mainArray => 
        // making sub requests here
        )


        Using mergeMap to turn array into Observable should work too, though it might be more confusing when diving 1 level deeper, imho. Anyway, the map does the main trick.



        Hope this helps






        share|improve this answer















        The main trick is to use map to merge scoped property with request result.



        Here's a rough example how to achieve this for the first level (specialProp):



        this.http.get(url).pipe(
        mergeMap(mainItemArr =>
        // forkJoin will wait for each request to complete
        return forkJoin(
        // make a subsequent request for each item in mainItemArr
        mainItemArr.map(mainItem =>
        return this.getSomeInfo(mainItem.Id).pipe(
        // merge getSomeInfo result with the mainItem
        map(someInfo =>
        return
        ...mainItem,
        specialProp: someInfo
        ;
        )
        )
        )
        )
        )
        )


        For the anotherSpecialProp requests — you'll need to go one level deeper.



        In a real world application I'd suggest splitting those subsequent calls into separate functions/methods.



        NOTE:



        You don't need turning array into Observable:



        mergeMap(mainArray => mainArray)


        Instead you might keep it in the JS scope and do subsequent requests in the mergeMap, e.g.:



        mergeMap(mainArray => 
        // making sub requests here
        )


        Using mergeMap to turn array into Observable should work too, though it might be more confusing when diving 1 level deeper, imho. Anyway, the map does the main trick.



        Hope this helps







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 26 at 13:34

























        answered Mar 26 at 12:09









        koskos

        2,0911 gold badge5 silver badges17 bronze badges




        2,0911 gold badge5 silver badges17 bronze badges























            3














            If I get it right, what you need to do is the following:



            • fetch the initial array from back end

            • for each element of the array, call getSomeInfo and store the result, which should be an array, into the specialProp property

            • then, for each entry in the specialProp array, you want to call the getSomeOtherInfo method, fetch some more data and store it into a property called anotherSpecialProp

            If all this is true, then you can try something along these lines



            getArray()
            .pipe(
            mergeMap(mainArray => mainArray), // unwind the array received
            switchMap(mainItem => getSomeInfo(mainItem.id) // fetch the first set of info from backend
            .pipe(
            tap(someInfo =>
            mainItem['specialProp'] = someInfo; // wrote someInfo into specialProp property
            ),
            mergeMap(specialProps => specialProps), // unwind the array of specialProps
            switchMap(specialProp => getSomeOtherInfo(specialProp.name) // for each specialProp fetch the additional data
            .pipe(
            tap(someOtherInfo =>
            specialProp['anotherSpecialProp'] = someOtherInfo // store additional data into anotherSpecialProp property
            )
            )
            ),
            toArray(), // rewind the array of specialProps and return it
            map(() => mainItem)
            )
            ),
            toArray() // rewind the array of mainItems and return it
            )


            The thing you may want to notice is the use of mergeMap with an Array, e.g. mergeMap(mainArray => mainArray).



            mergeMap accepts as input a function which returns an ObservableInput. An Array is an ObservableInput which emits all of its items synchronously before completing. So, passing a function which returns an Array to mergeMap means to emit all the elements of the Array.



            You can find an example of the above example here






            share|improve this answer























            • Thank you very much, guys! Your answers were very useful! It allowed me to get some understanding RxJs as I am a newbie in RxJs and a step to dive deeper into this library! thank you!

              – Vika
              Mar 28 at 8:07















            3














            If I get it right, what you need to do is the following:



            • fetch the initial array from back end

            • for each element of the array, call getSomeInfo and store the result, which should be an array, into the specialProp property

            • then, for each entry in the specialProp array, you want to call the getSomeOtherInfo method, fetch some more data and store it into a property called anotherSpecialProp

            If all this is true, then you can try something along these lines



            getArray()
            .pipe(
            mergeMap(mainArray => mainArray), // unwind the array received
            switchMap(mainItem => getSomeInfo(mainItem.id) // fetch the first set of info from backend
            .pipe(
            tap(someInfo =>
            mainItem['specialProp'] = someInfo; // wrote someInfo into specialProp property
            ),
            mergeMap(specialProps => specialProps), // unwind the array of specialProps
            switchMap(specialProp => getSomeOtherInfo(specialProp.name) // for each specialProp fetch the additional data
            .pipe(
            tap(someOtherInfo =>
            specialProp['anotherSpecialProp'] = someOtherInfo // store additional data into anotherSpecialProp property
            )
            )
            ),
            toArray(), // rewind the array of specialProps and return it
            map(() => mainItem)
            )
            ),
            toArray() // rewind the array of mainItems and return it
            )


            The thing you may want to notice is the use of mergeMap with an Array, e.g. mergeMap(mainArray => mainArray).



            mergeMap accepts as input a function which returns an ObservableInput. An Array is an ObservableInput which emits all of its items synchronously before completing. So, passing a function which returns an Array to mergeMap means to emit all the elements of the Array.



            You can find an example of the above example here






            share|improve this answer























            • Thank you very much, guys! Your answers were very useful! It allowed me to get some understanding RxJs as I am a newbie in RxJs and a step to dive deeper into this library! thank you!

              – Vika
              Mar 28 at 8:07













            3












            3








            3







            If I get it right, what you need to do is the following:



            • fetch the initial array from back end

            • for each element of the array, call getSomeInfo and store the result, which should be an array, into the specialProp property

            • then, for each entry in the specialProp array, you want to call the getSomeOtherInfo method, fetch some more data and store it into a property called anotherSpecialProp

            If all this is true, then you can try something along these lines



            getArray()
            .pipe(
            mergeMap(mainArray => mainArray), // unwind the array received
            switchMap(mainItem => getSomeInfo(mainItem.id) // fetch the first set of info from backend
            .pipe(
            tap(someInfo =>
            mainItem['specialProp'] = someInfo; // wrote someInfo into specialProp property
            ),
            mergeMap(specialProps => specialProps), // unwind the array of specialProps
            switchMap(specialProp => getSomeOtherInfo(specialProp.name) // for each specialProp fetch the additional data
            .pipe(
            tap(someOtherInfo =>
            specialProp['anotherSpecialProp'] = someOtherInfo // store additional data into anotherSpecialProp property
            )
            )
            ),
            toArray(), // rewind the array of specialProps and return it
            map(() => mainItem)
            )
            ),
            toArray() // rewind the array of mainItems and return it
            )


            The thing you may want to notice is the use of mergeMap with an Array, e.g. mergeMap(mainArray => mainArray).



            mergeMap accepts as input a function which returns an ObservableInput. An Array is an ObservableInput which emits all of its items synchronously before completing. So, passing a function which returns an Array to mergeMap means to emit all the elements of the Array.



            You can find an example of the above example here






            share|improve this answer













            If I get it right, what you need to do is the following:



            • fetch the initial array from back end

            • for each element of the array, call getSomeInfo and store the result, which should be an array, into the specialProp property

            • then, for each entry in the specialProp array, you want to call the getSomeOtherInfo method, fetch some more data and store it into a property called anotherSpecialProp

            If all this is true, then you can try something along these lines



            getArray()
            .pipe(
            mergeMap(mainArray => mainArray), // unwind the array received
            switchMap(mainItem => getSomeInfo(mainItem.id) // fetch the first set of info from backend
            .pipe(
            tap(someInfo =>
            mainItem['specialProp'] = someInfo; // wrote someInfo into specialProp property
            ),
            mergeMap(specialProps => specialProps), // unwind the array of specialProps
            switchMap(specialProp => getSomeOtherInfo(specialProp.name) // for each specialProp fetch the additional data
            .pipe(
            tap(someOtherInfo =>
            specialProp['anotherSpecialProp'] = someOtherInfo // store additional data into anotherSpecialProp property
            )
            )
            ),
            toArray(), // rewind the array of specialProps and return it
            map(() => mainItem)
            )
            ),
            toArray() // rewind the array of mainItems and return it
            )


            The thing you may want to notice is the use of mergeMap with an Array, e.g. mergeMap(mainArray => mainArray).



            mergeMap accepts as input a function which returns an ObservableInput. An Array is an ObservableInput which emits all of its items synchronously before completing. So, passing a function which returns an Array to mergeMap means to emit all the elements of the Array.



            You can find an example of the above example here







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 25 at 21:55









            PicciPicci

            5,9585 gold badges31 silver badges57 bronze badges




            5,9585 gold badges31 silver badges57 bronze badges












            • Thank you very much, guys! Your answers were very useful! It allowed me to get some understanding RxJs as I am a newbie in RxJs and a step to dive deeper into this library! thank you!

              – Vika
              Mar 28 at 8:07

















            • Thank you very much, guys! Your answers were very useful! It allowed me to get some understanding RxJs as I am a newbie in RxJs and a step to dive deeper into this library! thank you!

              – Vika
              Mar 28 at 8:07
















            Thank you very much, guys! Your answers were very useful! It allowed me to get some understanding RxJs as I am a newbie in RxJs and a step to dive deeper into this library! thank you!

            – Vika
            Mar 28 at 8:07





            Thank you very much, guys! Your answers were very useful! It allowed me to get some understanding RxJs as I am a newbie in RxJs and a step to dive deeper into this library! thank you!

            – Vika
            Mar 28 at 8:07

















            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%2f55340585%2fangular-rxjs-level-up-during-looping-an-array%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

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript