Angularfire:如何查询数组中特定键的值? [英] Angularfire: how to query the values of a specific key in an array?

查看:176
本文介绍了Angularfire:如何查询数组中特定键的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的json对象:

  {
posts:{
- Jk3ipZ2EFgvkABHf9IX:{
category:JavaScript,
date:1426008529445,
heading:Lorem,
text:dsvgfjds daefsrgfs defsds dsfsdds,
user:Mitko
},
-Jk3jFbOOE9V8Yd37tWj:{
category:C#,
date: 1426008640253,
heading:Lorem,
text:Some Text!,
user:Peter
}
}

我正在使用Angularfire作为我的应用程序。有没有办法让我查询这个JSON,只是提取类别名称,或者我应该为类别创建一个额外的对象和查询方式。我询问,因为它是多余的查询整个数组只有一个值。



在此先感谢

解决方案

Firebase的JavaScript SDK(在其上创建了AngularFire)将始终加载完整的节点。所以没有办法只从你的帖子加载类别。



常用的方法是创建第二个顶级节点,其中只包含类别;或者分类和(每个)该类别中每个帖子的推送标识。

  {
帖子:{
-Jk3ipZ2EFgvkABHf9IX:{
category:JavaScript,
date:1426008529445,
heading:Lorem,
text:dsvgfjds daefsrgfs defsds dsfsdds,
user:Mitko
},
-Jk3jFbOOE9V8Yd37tWj:{
category:C# ,
date:1426008640253,
heading:Lorem,
text:一些文本!,
user:Peter



$ C
$ b $ C
$ C




$ {
-Jk3jFbOOE9V8Yd37tWj
}
}

有点多余,但可以让你得到你需要的数据,以显示在您的用户界面中的特定屏幕。将这样的数据非规范化是过渡到NoSQL数据库的常见部分。


I have the following json object:

{
  "posts" : {
    "-Jk3ipZ2EFgvkABHf9IX" : {
      "category" : "JavaScript",
      "date" : 1426008529445,
      "heading" : "Lorem",
      "text" : "dsvgfjds daefsrgfs defsds dsfsdds",
      "user" : "Mitko"
    },
    "-Jk3jFbOOE9V8Yd37tWj" : {
      "category" : "C#",
      "date" : 1426008640253,
      "heading" : "Lorem",
      "text" : "Some Text!",
      "user" : "Peter"
    }
  }
}

I'm using Angularfire for my app. Is there a way for me to query this json and just extract the category names or am I supposed to create an additional object for the categories and query that way. I am asking because it feels redundant to query the whole array for just one value.

Thanks in advance

解决方案

Firebase's JavaScript SDK (on top of which AngularFire is built) will always load complete nodes. So there is no way to load just the category from your posts.

The common way around this is to create a second top-level node that contains just the categories; or alternatively the categories and (for each) the push-id of each post in that category.

{
  "posts" : {
    "-Jk3ipZ2EFgvkABHf9IX" : {
      "category" : "JavaScript",
      "date" : 1426008529445,
      "heading" : "Lorem",
      "text" : "dsvgfjds daefsrgfs defsds dsfsdds",
      "user" : "Mitko"
    },
    "-Jk3jFbOOE9V8Yd37tWj" : {
      "category" : "C#",
      "date" : 1426008640253,
      "heading" : "Lorem",
      "text" : "Some Text!",
      "user" : "Peter"
    }
  },
  "categories" : {
    "JavaScript" : {
      "-Jk3ipZ2EFgvkABHf9IX"
    },
    "C#" : {
      "-Jk3jFbOOE9V8Yd37tWj"
    }
}

It is a bit redundant, but allows you to get exactly the data you need to show a specific screen in your UI. Denormalizing your data like this is a common part of transitioning to a NoSQL database.

这篇关于Angularfire:如何查询数组中特定键的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆