Filter data from http provider

Hi, I can’t filter an array I get from an http. I use this provider to get data from my API. This data which is used to create a list, has a field “level_pre” which I like to use to filter data. In first page, I’ll show level_pre = 1 items, in page 2 I’ll show level_pre 2 items, and so on. First, my provider, that gets “preguntes” JSON, from API:

load(){
        if(this.data){
            return Promise.resolve(this.data);
        }
        return new Promise(
            resolve=>{
                this.http.get('/api'+'/preguntes.php')
                .map(res=>res.json())
                .subscribe(
                    data=>{
                        this.data=data;
                        resolve(this.data);
                    }
                );
            }
        );
    }

Then in page I have

constructor(public navCtrl: NavController, public navParams: NavParams, public preguntesprovider:PreguntesProvider, public loadingCtrl:LoadingController) {
      this.loading = this.loadingCtrl.create({
          content: '<ion-spinner ></ion-spinner>'
        });
      this.place = navParams.get('place');
      this.preguntesprovider.load()
        .then(
                data=>{
                    this.preguntes=data.preguntes;
                }
            );
      this.preguntes = this.preguntes.filter((item) => {
        return item.level_pre == '1';
    });
  }

I’d like to user param “place”, which is an objecte, get its level_pre, and filter “preguntes”, using this value, and show in page only this level_pre items. I’ve made some test, with a “1” value, but the filter doesn’t work. I think it’s something with object this.preguntes…

Thank you for your help,

1 Like

You are filtering something that doesn’t exist yet. The filter must be inside the then() block.

Yes, @rapropos, you’re right. Thank you!