简体   繁体   English

在Angular 2中嵌套json数据-如何过滤?

[英]Nested json data in Angular 2 - how to filter?

I'm new with Angular, Js etc, and have a problem with understanding how should work with nested data. 我是Angular,Js等人的新手,在理解应如何处理嵌套数据时遇到问题。 For example: 例如:

I have four json files: 我有四个json文件:

  • categories 分类
  • subcategories 子类别
  • posts 帖子
  • comments 评论

It's better to have 4 different files like above, or one like this: 最好有四个以上类似的文件,或者一个这样的文件:

{
"id_category":"1",
"name":"business",
"subcategories":{
    "id":"1",
    "name":"ecommerse",
    "posts": {
        "id": 1,
        "title": "New post"
        "comments": {
            "id": 1,
            "text": "text text text"
     }
  }
}

Of course it's only an example, but for that example I need to find comment by Id = 1 to get information about which post this comment is related to, which subcategory and category. 当然,这只是一个示例,但对于该示例,我需要按ID = 1查找注释,以获取有关此注释与哪个帖子,哪个子类别和类别有关的信息。

Now I have four different files, and services to get data from json files. 现在,我有四个不同的文件,以及从json文件获取数据的服务。 I can get a specific comment by ID: 我可以通过ID获得特定评论:

getComment(Id: number) {
    return this.comments.find(
        (comment) =>
        comment.id === Id 
    );
}

ok, fine. 好的。 But If I want to get information about post, subcategory, and main category for this comment? 但是,如果我想获得有关此评论的帖子,子类别和主要类别的信息,该如何做? What should I do? 我该怎么办?

It depends on what the specific needs of your application are. 这取决于您的应用程序的特定需求。

With this structure: 通过这种结构:

{
"id_category":"1",
"name":"business",
"subcategories":{
    "id":"1",
    "name":"ecommerse",
    "posts": {
        "id": 1,
        "title": "New post"
        "comments": {
            "id": 1,
            "text": "text text text"
     }
  }
}

You could iterate over Categories and display a list, then allow a user to select a single category , and assign that to var currentCategory . 您可以遍历Categories并显示一个列表,然后允许用户选择一个category ,然后将其分配给var currentCategory

You could then iterate over currentCategory.subcategories to allow the user to select a subcategory and assign that to var currentSubCategory . 然后,您可以遍历currentCategory.subcategories以允许用户选择一个subcategory并将其分配给var currentSubCategory You would keep drilling down then into currentSubCategory.posts , allow the user to select a post , assign that to var currentPost and then iterate over that to display currentPost.comments . 然后,您将继续向下钻取到currentSubCategory.posts ,允许用户选择一个post ,将其分配给var currentPost ,然后对其进行迭代以显示currentPost.comments

If you're fetching from a database in order to allow the user to drill down into the data for display only, then something like this would work. 如果要从数据库中获取数据以允许用户向下钻取仅用于显示的数据,则可以使用类似的方法。

If you're maintaining data in JSON files, then I would look at something like JSON Server https://github.com/typicode/json-server 如果您要维护JSON文件中的数据,那么我会看一下类似JSON服务器的内容https://github.com/typicode/json-server

If you're building something more substantial and you have a database backend, make use of the database and don't try to recreate that functionality in your JSON, use JSON as a transport for the data, but don't try to replicate entire tables or complex data structures in your front end code, just fetch what you need as you need it, as that will make for a much more stable and scalable application, and will also make it easier for you do fetch and update data in small manageable chunks. 如果您正在构建更重要的东西,并且具有数据库后端,请使用数据库,并且不要尝试在JSON中重新创建该功能,请使用JSON作为数据的传输方式,但不要尝试复制整个数据库前端代码中的表或复杂数据结构,只需按需获取所需内容即可,因为这将使应用程序更加稳定和可扩展,并且还使您更容易以小型可管理的方式获取和更新数据大块。

For mocking a data backend using JSON, consider json-server 为了使用JSON模拟数据后端,请考虑json-server

https://www.npmjs.com/package/json-server https://www.npmjs.com/package/json-server

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM