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

464
Visualizações
Add snapshot.val & snapshot.key to an array while I´m subscribed

I´m gonna break my head with a stone ^^"
I have this code:

            this.af.database.list('/Documentos', { preserveSnapshot: true })
            .subscribe(snapshots => {
                snapshots.forEach(snapshot => {
                    console.log(snapshot.key, snapshot.val());
                });
            })

With that I extract all the data correctly, but now I want to add to an object array or something like that (I started few weeks ago with Firebase + Angular2).
I wanna fill that array to load the [ng2 smart table] and if I´m thinking partially well with a properly well-formed array I will fill the table but I don´t know how. Hope anyone can help.

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

0

If you want an map (object) with key: value, you can easily do this with Array.prototype.reduce():

const map = snapshots.reduce((map, snapshot) => {
    map[snapshot.key] = snapshot.val();
}, {});
over 4 years ago · Santiago Trujillo Relatório

0

Well, according to the example : https://akveo.github.io/ng2-smart-table/#/examples/using-filters ...

(You can find the source code here: https://github.com/akveo/ng2-smart-table/blob/master/src/app/pages/examples/filter/basic-example-source.component.ts)

... you have to put your data in a JSON object :

settings = {
  columns: {
    id: {
      title: 'ID',
      filter: false,
    },
    name: {
      title: 'Full Name',
      filter: false,
    },
    username: {
      title: 'User Name',
      filter: false,
    },
    email: {
      title: 'Email',
      filter: false,
    }
  }
};

data = [
  {
    id: 1,
    name: 'Leanne Graham',
    username: 'Bret',
    email: 'Sincere@april.biz',
  },
  {
    id: 2,
    name: 'Ervin Howell',
    username: 'Antonette',
    email: 'Shanna@melissa.tv',
  }
];

"settings" contain your columns names and "data" must match the columns from "settings".

It would be easier if we knew a bit more of your code (columns of your table + data returned by your service), but I assume something like that would work :

data = [];
this.af.database.list('/Documentos', { preserveSnapshot: true })
        .subscribe(snapshots => {
            snapshots.forEach(snapshot => {
                data.push(
                    { [snapshot.key]: snapshot.val() }
                );
            });
        })

Please note that this will create a JSON array with only one key/val per row. We do need to know more about your data to give you a propre answer.

over 4 years ago · Santiago Trujillo Relatório

0

Ok, I found the solution with a simple Array() x)

            this.af.database.list('/Documentos', { preserveSnapshot: true })
            .subscribe(snapshots => {
                snapshots.forEach(snapshot => {
                    let length = todo.documentos.push(snapshot.val()); // documentos is an array in the class
                    todo.source.load(todo.documentos);
                });
            });
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