Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

467
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda