Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

100
Visualizações
RxJS Loop httpGet result then do httpPut and httpPost on different scenario

I have a UI form that to update or add translation text.

I would need to write rxjs statement to fulfill the following:

  1. Fire httpGet to database to get translations of multiple languages

  2. Loop the list of translations.

  3. If my UI fields matches translations from httpGet, I would need to do a series of httpPut to update them in database. Example: I filled in Spanish translation in UI, if my httpGet returns results including Spanish, I would need to HttpPut to update Spanish record in database.

  4. If my UI fields does not exist in translations from httpGet, I would need to do a series of httpPost to update them in database. Example: I filled in Chinese translation in UI, if my httpGet does not return any Chinese result, I would need to httpPost to add Chinese record in database.

I written the first part, but understand that rxjs is not sequential and cannot write a subscribe in another subscribe, I cannot write it in this way.

p/s: My httpGet returns me x, in which has the structure of x.dataCount and x.dataSet. x.dataCount is number of translation object. x.dataSet is an array of translation objects.

How do I write the rxjs statement for this? Any clue?

this.GetTranslations(group).subscribe(x => {
  x.dataSet.forEach(translation => {
    switch (translation.Language) {
      case chineseCode:
        if (chineseInUI){
          chineseExist = true;
          let newChinese = {
            "Id": translation.Id,
            "Language": translation.Language,
            "PhraseKey": translation.PhraseKey,
            "PhraseValue": chineseInUI,
          }
          this.webApiService.httpPut$(this.resourceApiService.getURL('translationmanagerURL') + translation.Id, newChinese).subscribe();            }
        break;
      case spanishCode:
        if (spanishInUI){
          let newSpanish = {
            "Id": translation.Id,
            "Language": translation.Language,
            "PhraseKey": translation.PhraseKey,
            "PhraseValue": spanishInUI
          }
          this.webApiService.httpPut$(this.resourceApiService.getURL('translationmanagerURL') + translation.Id, newSpanish).subscribe();
        }
        break;
    }
  });
});
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

You can do it like this using operators

  • mergeMap to create an array of observables
  • mergeAll to get the values of the inner observables in the subscription
   this.GetTranslations(group)
      .pipe(
        mergeMap((x) => {
          return x.dataSet.map((translation) => {
            switch (translation.Language) {
              case chineseCode:
                if (chineseInUI) {
                  chineseExist = true;
                  let newChinese = {
                    Id: translation.Id,
                    Language: translation.Language,
                    PhraseKey: translation.PhraseKey,
                    PhraseValue: chineseInUI,
                  };
                  return this.webApiService.httpPut$(
                    this.resourceApiService.getURL('translationmanagerURL') +
                      translation.Id,
                    newChinese
                  );
                }
                break;
              case spanishCode:
                if (spanishInUI) {
                  let newSpanish = {
                    Id: translation.Id,
                    Language: translation.Language,
                    PhraseKey: translation.PhraseKey,
                    PhraseValue: spanishInUI,
                  };
                  return this.webApiService.httpPut$(
                    this.resourceApiService.getURL('translationmanagerURL') +
                      translation.Id,
                    newSpanish
                  );
                }
                break;
            }

            return EMPTY;
          });
        }),
        mergeAll()
      )
      .subscribe((response) => {
        console.log('response', response);
      });

Working example (with mocked data)

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda