Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 如何筛选已从firebase检索到的数据?_Typescript_Firebase_Ionic2 - Fatal编程技术网

Typescript 如何筛选已从firebase检索到的数据?

Typescript 如何筛选已从firebase检索到的数据?,typescript,firebase,ionic2,Typescript,Firebase,Ionic2,我在“/posts/”下有一个帖子列表,我检索了所有帖子并将其存储在数组中所有帖子: get_all_posts(){ this.all_posts = []; firebase.database().ref('/posts/').once('value').then(snapshot => { foreach.snapshot{ let tmp_data = { post_id: snapshot.key, title: snapshot

我在“/posts/”下有一个帖子列表,我检索了所有帖子并将其存储在数组中所有帖子:

get_all_posts(){

 this.all_posts = [];
 firebase.database().ref('/posts/').once('value').then(snapshot => {

 foreach.snapshot{
    let tmp_data = {
        post_id: snapshot.key,
        title: snapshot.val().title,
        type: snapshot.val().type,
        }   
    this.all_posts.push(tmp_data);
 }

}
现在我想通过点击按钮过滤type=“news”(有两种类型:新闻和博客)的帖子

<ion-button (click)="show_only_posts_type_news()">show only news</ion-button>

show_only_posts_types_news(){

 //this.posts
 // filter only type == "news"

}
仅显示新闻
仅显示文章类型新闻(){
//这是我的帖子
//仅筛选器类型==“新闻”
}
有没有更简单的方法来过滤所有的帖子?如果没有另一个firebase查询仅显示新闻类型,则可以使用。您可以提供一个筛选函数,该函数将只计算并返回满足您的条件的元素

var news_posts = this.all_posts.filter(post => post.type === 'news')
你可以用。您可以提供一个筛选函数,该函数将只计算并返回满足您的条件的元素

var news_posts = this.all_posts.filter(post => post.type === 'news')

由于您已经循环浏览快照,因此最好在该循环中进行过滤,而不是再次循环浏览所有_帖子以获取新闻,并可能在以后获取博客,而array.filter在后台就是这样做的

get_all_posts(){

this.news_posts = [];
this.blogs_posts = [];
firebase.database().ref('/posts/').once('value').then(snapshot => {

foreach.snapshot{ 
 let tmp_data = {
        post_id: snapshot.key,
        title: snapshot.val().title,
        type: snapshot.val().type,
         }  
  if(tmp_data.type === 'news'
  {
    this.news_posts(tmp_data)
  }
  else
  {
    this.blogs_posts(tmp_data)
  }
 }
}

由于您已经循环浏览快照,因此最好在该循环中进行过滤,而不是再次循环浏览所有_帖子以获取新闻,并可能在以后获取博客,而array.filter在后台就是这样做的

get_all_posts(){

this.news_posts = [];
this.blogs_posts = [];
firebase.database().ref('/posts/').once('value').then(snapshot => {

foreach.snapshot{ 
 let tmp_data = {
        post_id: snapshot.key,
        title: snapshot.val().title,
        type: snapshot.val().type,
         }  
  if(tmp_data.type === 'news'
  {
    this.news_posts(tmp_data)
  }
  else
  {
    this.blogs_posts(tmp_data)
  }
 }
}
非常感谢@Ashish(对于Array.filter函数,它提供了许多过滤数据的方法)。 @Mocas我以后会有更多类型的帖子,你的方法也是可行的

最后我做了这个,一个功能是在点击按钮时过滤帖子类型,不确定它是否是最好的,但按照预期工作:

我首先分配了3个阵列:

rows = [];          //the row that contains displaying data on the view
all_rows = [];      //the row that contains all the posts
filtered_rows = []; //the row that contains only filtered posts
过滤器功能:

filter_posts_by_type(type){

    this.filtered_rows = this.all_rows; 
    //reset the filtered_rows with all data first

    if(type == "news"){

        this.rows = this.filtered_rows.filter((item) => {
            return item.type.toLowerCase() == "news"; 
            //set this.rows to contain all posts of type : news
        })

    }else if(type == "blog"){ 

        this.rows = this.filtered_rows.filter((item) => {
            return item.type.toLowerCase() == "blog"; 
           //set this.rows to contain all posts of type : blog

        })
    }else{
        // all types
        this.rows = this.filtered_rows.filter((item) => {
            return item.type.toLowerCase() == "news" || item.type.toLowerCase() == "blog"; 
           //set this.rows to contain all posts types
        })
    }
}
非常感谢@Ashish(对于Array.filter函数,它提供了许多过滤数据的方法)。 @Mocas我以后会有更多类型的帖子,你的方法也是可行的

最后我做了这个,一个功能是在点击按钮时过滤帖子类型,不确定它是否是最好的,但按照预期工作:

我首先分配了3个阵列:

rows = [];          //the row that contains displaying data on the view
all_rows = [];      //the row that contains all the posts
filtered_rows = []; //the row that contains only filtered posts
过滤器功能:

filter_posts_by_type(type){

    this.filtered_rows = this.all_rows; 
    //reset the filtered_rows with all data first

    if(type == "news"){

        this.rows = this.filtered_rows.filter((item) => {
            return item.type.toLowerCase() == "news"; 
            //set this.rows to contain all posts of type : news
        })

    }else if(type == "blog"){ 

        this.rows = this.filtered_rows.filter((item) => {
            return item.type.toLowerCase() == "blog"; 
           //set this.rows to contain all posts of type : blog

        })
    }else{
        // all types
        this.rows = this.filtered_rows.filter((item) => {
            return item.type.toLowerCase() == "news" || item.type.toLowerCase() == "blog"; 
           //set this.rows to contain all posts types
        })
    }
}

感谢Mocas,我找到了一种使用array.filter方法的方法,我在下面回答了这个问题,但我认为在快照期间对帖子进行分组似乎是一种更好的方法,正如您提到的,我不必再次进行筛选。再次感谢:)感谢Mocas,我找到了一种使用array.filter方法的方法,我在下面回答了这个方法,但我认为在快照期间对帖子进行分组似乎是一种更好的方法,正如你提到的,我不必再进行筛选。再次感谢:)感谢ashish的回答,我现在找到了一种方法来过滤检索到的数据(在快照时过滤),适合我目前的情况,但是您的答案对于涉及更多参数的更复杂搜索非常有用。再次感谢:)感谢ashish的回答,我现在找到了一种过滤检索到的数据(在快照时过滤)的方法,适合我目前的情况,但您的回答对于涉及更多参数的更复杂搜索非常有用。再次感谢:)