从Typescript角度的json对象中获取数组

问题描述 投票:0回答:1

我有一个json对象,它由字符串元素以及一个array类型的元素组成。我不知道如何获取object的json对象。以下是我的代码段。

getData(){
    this.productService.getliveData().subscribe(resp => {
      this.catDta = resp['categories'];
      const products: any = [];
      const jsonobject = resp['categories'];
      this.newestItems=jsonobject['cat_products'];
      console.log('newwest items2',jsonobject['cat_products']);

    });
  }

我只想知道如何从类别对象中获取类型为数组的cat_products。

任何帮助将不胜感激。

下面是json

{
            "id": "3",
            "title": "Suspension",
            "parent_id": "1",
            "deleted": "0",
            "cat_image": "https://www.insertcart.com/wp-content/uploads/2016/09/category.png",
            "expanded": false,
            "**cat_products**": [
                {
                    "id": "7",
                    "title": "Suspension Rodes",
                    "description": "",
                    "cat_id": "3",
                    "unit_type": "",
                    "item_type": "product",
                    "rate": "12",
                    "deleted": "0",
                    "workplace_id": null,
                    "stock": null,
                    "product_image": "https://i.insider.com/5e69490554f25c19106866f2?width=700&format=jpeg&auto=webp",
                    "amount": 1,
                    "sale": true,
                    "featured": true,
                    "fav": false,
                    "res": true
                },
                {
                    "id": "2",
                    "title": "Brake Pads",
                    "description": "Testing",
                    "cat_id": "3",
                    "unit_type": "No",
                    "item_type": "product",
                    "rate": "10",
                    "deleted": "0",
                    "workplace_id": "2",
                    "stock": "5",
                    "product_image": "https://i.insider.com/5e69490554f25c19106866f2?width=700&format=jpeg&auto=webp",
                    "amount": 1,
                    "sale": true,
                    "featured": true,
                    "fav": false,
                    "res": true
                },
                {
                    "id": "2",
                    "title": "Brake Pads",
                    "description": "Testing",
                    "cat_id": "3",
                    "unit_type": "No",
                    "item_type": "product",
                    "rate": "10",
                    "deleted": "0",
                    "workplace_id": "3",
                    "stock": "1",
                    "product_image": "https://i.insider.com/5e69490554f25c19106866f2?width=700&format=jpeg&auto=webp",
                    "amount": 1,
                    "sale": true,
                    "featured": true,
                    "fav": false,
                    "res": true
                }
            ]
        },

我需要访问cat_products。请帮助我

angular ionic-framework multidimensional-array angular-material
1个回答
0
投票

如果resp是json结构中的对象,那么您可以通过以下方式访问其产品:

const products = resp['cat_products'];
© www.soinside.com 2019 - 2024. All rights reserved.