0

I can easily get the data working in my HTML, but when I have to convert it for visualization purposes it is a struggle.

I get the data as a FirebaseListObservable. In my case there's 3 value-types in each List, but it is only one of them there has to be part of the array. How do I convert a FirebaseListObservable to an array in typescript?

The reason of converting is to use graphs in my app.

Typescript getting the data:

this.measure= this.db.list('/Users/'+this.username+'/Measure');
enter code here

Typescript for chartjs

this.data = [12, 24, 91, 23] 

The data has to be the data from the firebase

2
  • posting with some code will increase the chance of getting answered. Commented Nov 13, 2017 at 19:53
  • @Hareesh -I have updated it. Thanks for mentioning. Commented Nov 13, 2017 at 20:26

2 Answers 2

2

Declare a empty array data=[];

then get the data with .subscribe()

this.db.list('/Users/'+this.username+'/Measure').subscribe(measures => {
    measures.forEach(measure => {
      this.data.push(measure.number);//.number should be your field in firebase
    })
  })

this will fetch all list and search through all items and push your desired values to the array.

Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I needed!
0

FirebaseListObservable is an observable. That means you will get the list itself (=the array you want) when you subscribe to it.

this.measure: FirebaseListObservable<Measure[]> = this.db.list('/Users/'+this.username+'/Measure');
this.measure
    .map(measurement => measurement.numbers) // this extracts only numbers field so we have basically array of arrays
    .map(numbersArrays => numbersArrays.reduce((curr, prev) => curr.concat(prev))) // so lets concat all those arrays into a single one
    .subscribe(numbersArray: number[] => {
        console.log(numbersArray);
    });

interface Measure {
    data: Date;
    geolocation: string;
    numbers: number[];
}

6 Comments

I will try that, thanks. Measure holds both the numbers, date and geolocation.
I've added interface for Measure
Okay so I got this now: this.db.list('/Users/'+this.username+'/Measure').subscribe(items => { console.log(items); }); I can see the data in my console. How do I save the numbers in an array?
So what do you want to do with those measurements now? There are numbers on each item of that list. Do you want to just join them together?
Yes, that is exactly what I want. Like this [30,40,20,10,30]
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.