Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

463
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!