首页 > 解决方案 > 从 json 对象中分离一些值(react,jquery)

问题描述

在反应的应用程序中,我有这个 json 数据来自var product

我正在尝试从 json 下面存储“类别名称”

喜欢

category: 'Jumpsuits,Cargo Jumpsuit' // this is the desired output

product = {
  "isConfigurableProduct": true,
  "id": "bdg-workwear-denim-boiler-suit",
  "categories": [
    {
      "id": 2437,
      "name": "Jumpsuits",
      "url_key": "jumpsuits",
      "level": 4,
      "url_suffix": ".html",
      "breadcrumbs": [
        {
          "category_id": 3,
          "category_level": 2,
          "category_name": "Clothing",
          "category_url_key": "clothing",
          "category_url_path": "clothing"
        },
        {
          "category_id": 82,
          "category_level": 3,
          "category_name": "Jumpsuits & Rompers",
          "category_url_key": "jumpsuits-rompers",
          "category_url_path": "clothing/jumpsuits-rompers"
        }
      ],
      "path": "clothing/jumpsuits-rompers"
    },
    {
      "id": 2492,
      "name": "Cargo Jumpsuit",
      "url_key": "cargo-jumpsuit",
      "level": 5,
      "breadcrumbs": [
        {
          "category_id": 3,
          "category_name": "Clothing",,
          "category_url_path": "clothing"
        },
        {
          "category_id": 82,
          "category_name": "Jumpsuits & Rompers",
          "category_url_path": "clothing/jumpsuits-rompers"
        },
        {
          "category_id": 2437,
          "category_name": "Jumpsuits",
          "category_url_key": "jumpsuits",
        }
      ],
      "path": "clothing/jumpsuits-rompers/jumpsuits"
    }
  ]
}

他们有什么办法可以将它与这个 json 分开吗?获取它的优化方式是什么?任何想法,感谢

标签: jquery

解决方案


您可以迭代并将类别名称推送到单独的数组并将它们分开。-


const product = {
    "isConfigurableProduct": true,
    "id": "bdg-workwear-denim-boiler-suit",
    "categories": [
      {
        "id": 2437,
        "name": "Jumpsuits",
        "url_key": "jumpsuits",
        "level": 4,
        "url_suffix": ".html",
        "breadcrumbs": [
          {
            "category_id": 3,
            "category_level": 2,
            "category_name": "Clothing",
            "category_url_key": "clothing",
            "category_url_path": "clothing"
          },
          {
            "category_id": 82,
            "category_level": 3,
            "category_name": "Jumpsuits & Rompers",
            "category_url_key": "jumpsuits-rompers",
            "category_url_path": "clothing/jumpsuits-rompers"
          }
        ],
        "path": "clothing/jumpsuits-rompers"
      },
      {
        "id": 2492,
        "name": "Cargo Jumpsuit",
        "url_key": "cargo-jumpsuit",
        "level": 5,
        "breadcrumbs": [
          {
            "category_id": 3,
            "category_name": "Clothing",
            "category_url_path": "clothing"
          },
          {
            "category_id": 82,
            "category_name": "Jumpsuits & Rompers",
            "category_url_path": "clothing/jumpsuits-rompers"
          },
          {
            "category_id": 2437,
            "category_name": "Jumpsuits",
            "category_url_key": "jumpsuits",
          }
        ],
        "path": "clothing/jumpsuits-rompers/jumpsuits"
      }

    ]}

let sub_category_names = []
let category_names = []
product.categories.forEach(function (category) {
category_names.push(category.name)

 category.breadcrumbs.forEach(function (item) {
   sub_category_names.push(item.category_name)
});
});


 // for convert to string
let category_str = category_names.toString();
let str = sub_category_names.toString();
console.log("Main Categories are",category_str);
  console.log("Sub Cateogries are ",str);

推荐阅读