简体   繁体   English

用角度连接两个json文件的最佳方法是什么?

[英]What is the best way to join two json files in angular?

I have a json file which contains the list of products. 我有一个包含产品列表的json文件。

[{"id":76,
  "name":"A",
  "description":"abc",
  "price":199,
  "imageUrl":"image.jpg",
  "productCategory":[{
    "categoryId":5,
    "category":null
   },{
    "categoryId":6,
    "category":null
   }
]}

I then have a second json file with a list of categories which look like so: 然后,我有了第二个json文件,其中包含如下所示的类别列表:

[{"id":5,"name":"red"},
{"id":6,"name”:"blue"}]

What is the best way to join the categories of this two json files in Angular? 在Angular中加入这两个json文件类别的最佳方法是什么? This is what I aim to achieve: 这是我要实现的目标:

[{"id":76,
  "name":"A",
  "description":"abc",
  "price":199,
  "imageUrl":"image.jpg",
  "productCategory":[{
    "categoryId":5,
    "category":red
   },{
    "categoryId":6,
    "category":blue
   }
]}

You can use filter function for your requirement as below 您可以按照以下要求使用过滤器功能

let products = [{
      "id": 76,
      "name": "A",
      "description": "abc",
      "price": 199,
      "imageUrl": "image.jpg",
      "productCategory": [{
        "categoryId": 2,
        "category": null
      }, {
        "categoryId": 1,
        "category": null
      }]
    }, {
      "id": 77,
      "name": "B",
      "description": "abcd",
      "price": 1997,
      "imageUrl": "image.jpg",
      "productCategory": [{
        "categoryId": 5,
        "category": null
      }, {
        "categoryId": 6,
        "category": null
      }]
    },
    {
      "id": 78,
      "name": "C",
      "description": "abcde",
      "price": 1993,
      "imageUrl": "image.jpg",
      "productCategory": [{
        "categoryId": 4,
        "category": null
      }, {
        "categoryId": 6,
        "category": null
      }]
    }];


    let category = [{ "id": 5, "name": "red" }, { "id": 6, "name": "blue" }]

    let result = products.filter(p => {
      var exist = p.productCategory.filter(pc => category.find(c => c.id == pc.categoryId))[0];

      return exist;
    });

    console.log(result);

 let products = [{ "id": 76, "name": "A", "description": "abc", "price": 199, "imageUrl": "image.jpg", "productCategory": [{ "categoryId": 2, "category": null }, { "categoryId": 1, "category": null }] }, { "id": 77, "name": "B", "description": "abcd", "price": 1997, "imageUrl": "image.jpg", "productCategory": [{ "categoryId": 5, "category": null }, { "categoryId": 6, "category": null }] }, { "id": 78, "name": "C", "description": "abcde", "price": 1993, "imageUrl": "image.jpg", "productCategory": [{ "categoryId": 4, "category": null }, { "categoryId": 6, "category": null }] }]; let category = [{ "id": 5, "name": "red" }, { "id": 6, "name": "blue" }] let result = products.filter(p => { var exist = p.productCategory.filter(pc => category.find(c => c.id == pc.categoryId))[0]; return exist; }); console.log(result); 

I make a stackblitz that use a service to retreive the data. 我使用服务来检索数据进行了堆栈闪电 Yes, the way is using switchMap and map. 是的,方法是使用switchMap和map。 SwitchMap receive an array and must return an observable. SwitchMap接收一个数组,并且必须返回一个可观察的对象。 with map, we transform the data received and return the data transformed 使用map,我们转换接收到的数据并返回转换后的数据

this.dataService.getCategories().pipe(
         //first get the categories, the categories is in the
         //variable cats
         switchMap((cats:any[])=>{
           return this.dataService.getProducts().pipe(map((res:any[])=>{
               res.forEach(p=>{ //with each product
                 p.productCategory.forEach(c=>{ //with each productCategory in product
                   //equals a propertie "category" to the propertie "name" of the cats
                   c.category=cats.find(x=>x.id==c.categoryId).name 
                 })
               })
               return res
           }))
     })).subscribe(res=>{
       console.log(res)
     })

If only has an unique product we can make 如果只有独特的产品,我们可以制造

this.dataService.getCategories().pipe(
         switchMap((cats:any[])=>{
           return this.dataService.getUniqProduct(2).pipe(map((res:any)=>{
                 res.productCategory.forEach(c=>{
                   c.category=cats.find(x=>x.id==c.categoryId).name
                 })
               return res
           }))
     })).subscribe(res=>{
       console.log(res)
     })

We can improve our dataService "cached" the categories 我们可以改善我们的dataService“缓存”类别

  getCategories() {
    if (this.categories)
      return of(this.categories);

    return http.get(......).pipe(tap(res=>{
       this.categories=res;
    }))
  }

NOTE:In the stackbit I simulate the call to an http.get(...) using "of" 注意:在stackbit中,我使用“ of”模拟对http.get(...)的调用

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

相关问题 实现与JS对象“连接”的最佳方法是什么? - What is the best way to realise “join” with JS objects? 用JSON进行角度两种方式绑定? - angular two way binding with JSON? 在Angular(PhoneGap)中,构造AJAX调用并将返回值用作可存储JSON数据的最佳方法是什么? - In Angular (PhoneGap), what's the best way to structure an AJAX call and use the return as storable JSON data? 在d3.json中使用d3.csv合并多个csv文件数据输入的最佳方法是什么? - What is the best way to combine multiple csv files data input using d3.csv in d3.json ? 在nodejs中的两个html文件之间访问共享javascript变量的最佳方法是什么 - what is the best way to access the shared javascript variable between two html files in nodejs 处理在两个(或多个)缓冲区块之间拆分的大型 JSON 的最佳方法是什么 - What is the best way to work with a large JSON splitted between two (or more) buffer chunks 在Angular 2中复制对象的最佳方法是什么? - What is the best way to copy objects in Angular 2? 在Angular控制器中定义常数的最佳方法是什么? - What is the best way to define a constant in an Angular controller? 在Angular中倒退时间的最佳方法是什么 - What is the best way to get time backwards in Angular Angular 遍历嵌套的 object 的最佳方法是什么 - Angular what is the best way to traverse the nested object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM