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

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

问题描述

我有以下 json 对象:

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"
    }
  }
}

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

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.

提前致谢

推荐答案

Firebase 的 JavaScript SDK(AngularFire 在其之上构建)将始终加载完整节点.因此,无法仅从您的帖子中加载类别.

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.

解决这个问题的常用方法是创建一个仅包含类别的第二个顶级节点;或者,类别和(对于每个)该类别中每个帖子的推送 ID.

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"
    }
}

它有点多余,但可以让您准确获取在 UI 中显示特定屏幕所需的数据.像这样对数据进行非规范化是过渡到 NoSQL 数据库的常见部分.

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天全站免登陆