【问题标题】:How to loop through json object with specific key and value in javascript?如何在javascript中循环使用特定键和值的json对象?
【发布时间】:2018-09-11 02:03:14
【问题描述】:

我正在循环一个 json 对象,其结构如下:

var my_products = {
    "products": [
        {
            "title": "Product 1",
            "id": 001,
            "stock_quantity": 0,
            "in_stock": false
        },
        {
            "title": "Product 2",
            "id": 002,
            "stock_quantity": 0,
            "in_stock": false
        },
        {
            "title": "Product 3",
            "id": 003,
            "stock_quantity": 1,
            "in_stock": true
        },
        {
            "title": "Product 4",
            "id": 004,
            "stock_quantity": 1,
            "in_stock": true
        }
    ]
};

我可以使用下面的代码遍历对象:

for(var i=0; i<my_products.products.length; i++) {
    var product = my_products.products[i];
    console.log(product);
    console.log('in_stock:',product.in_stock);
}

您知道如何循环遍历具有"stock_quantity": 1 and "in_stock": true 属性的产品的每条记录吗?

在上面的示例中,有 2 个产品记录具有“stock_quantity": 1 属性(非零数量)

【问题讨论】:

    标签: javascript php jquery wordpress woocommerce


    【解决方案1】:

    你可以先filter数组:

    var my_products = {
        "products": [
            {
                "title": "Product 1",
                "id": 001,
                "stock_quantity": 0,
                "in_stock": false
            },
            {
                "title": "Product 2",
                "id": 002,
                "stock_quantity": 0,
                "in_stock": false
            },
            {
                "title": "Product 3",
                "id": 003,
                "stock_quantity": 1,
                "in_stock": true
            },
            {
                "title": "Product 4",
                "id": 004,
                "stock_quantity": 1,
                "in_stock": true
            }
        ]
    };
    console.log(
      my_products.products.filter(({ stock_quantity, in_stock }) => (
        in_stock && stock_quantity === 1
      ))
    );

    【讨论】:

      【解决方案2】:

      您只需使用if 条件迭代和filter 数据。

      var my_products = {
          "products": [
              {
                  "title": "Product 1",
                  "id": 001,
                  "stock_quantity": 0,
                  "in_stock": false
              },
              {
                  "title": "Product 2",
                  "id": 002,
                  "stock_quantity": 0,
                  "in_stock": false
              },
              {
                  "title": "Product 3",
                  "id": 003,
                  "stock_quantity": 1,
                  "in_stock": true
              },
              {
                  "title": "Product 4",
                  "id": 004,
                  "stock_quantity": 1,
                  "in_stock": true
              }
          ]
      };
      var filterData = [];
      $.each(my_products.products, function(){
          if (this.in_stock && this.stock_quantity === 1) {
              filterData.push(this);
          }
      });
      console.log(filterData);
      &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

      【讨论】:

        猜你喜欢
        • 2021-12-05
        • 1970-01-01
        • 2020-10-28
        • 1970-01-01
        • 2018-09-22
        • 1970-01-01
        • 2021-11-03
        • 2022-01-24
        • 1970-01-01
        相关资源
        最近更新 更多