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

442
Visualizações
How to flatten Array of Observables in an Observable

I'm trying to flatten a nested Observable, but I'm not getting it to work:

 this.af.object('test/father')
  .map(res => {
    res.namedKeys = [];
    for (let el in res.keys) {
      res.namedKeys.push(this.af.object(`test/keys/${el}`))
    }
    return res
  })
  .flatMap(res => Observable.combineLatest(res))
  .subscribe(res => {
    console.log('The final response:', res);
  });

I want to get the actual data of the Observables in my Array. I've tried a lot of different things the last 2 days, but in the end, I receive either nothing or still the Array with Observables in it. The code above is returning nothing.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

It's pretty hard to see what is the problem , since you don't supply any structure info.

I think you mean :

 for (let el in Object.keys(res))

instead of

 for (let el in res.keys) 

Also - combineLatest expects an observable. you didn't supply one :

Consider using this instead :

.flatMap(res => Rx.Observable.combineLatest(Rx.Observable.of(res)))

Demo ( which uses rxjs 5 which uses switchmap instead of flatMap)
http://jsbin.com/cuvisecipi/1/edit?html,js,console,output

A better demo which illustrates a similar ( from an array) solution :

http://jsbin.com/punoxitino/1/edit?html,js,console,output

over 4 years ago · Santiago Trujillo Relatório

0

The .combineLatest operator would work but it expects an array of Observables. You're passing it an object res that contains a property with an array of Observables.

In other words .combineLatest needs this: Observable[]
But you're passing it: { namedKeys: Observable[] }

So you problem is not simply flattening Observables (like this eg. Combine multiple observable arrays into new object array) because you need to pass the original objects as well. I'd recommend using forkJoin because it collects all items from all source Observables until they complete.

this.af.object('test/father')
  .concatMap(res => {
    // the original object is here as well at index 0 
    observables = [ Observable.of(res) ]; 
    for (let el in res.keys) {
      observables.push(this.af.object(`test/keys/${el}`))
    }
    return Observable.forkJoin(observables);
  })
  .map(([res, ...namedKeys]) => {
    res.namedKeys = namedKeys;
    return res;
  })
  .subscribe(res => { ... });
over 4 years ago · Santiago Trujillo 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