如何通过REACT JS在循环中分别为JSON对象的每个键调用API请求?

时间:2019-05-04 09:44:03

标签: json reactjs api loops

我有 JSON对象数据,存储在 mycart 状态下,格式如下: (这是按产品的公司名称分组的产品数据)

{
        "Master Automotives": [
            {
                "SparePartID": "43",
                "Name": "Oil and Lubricants",
                "Price": "4500",
                "VendorID": "48",
                "CompanyName": "Master Automotives",
                 "Qty": 1,
                 "TotalPrice": "4500"

            },
            {
                "SparePartID": "45",
                "Name": "Lights",
                "Price": "2300",
                "VendorID": "48",
                "CompanyName": "Master Automotives",
                 "Qty": 1,
                 "TotalPrice": "2300"
            }
        ],

        "Repair Solutions": [
            {
                "SparePartID": "47",
                "Name": "Steering Wheel",
                "Price": "1500",
                "VendorID": "60",
                "CompanyName": "Repair Solutions",
                 "Qty": 1,
                 "TotalPrice": "1500"
            }
        ],
        

         "FiveStar Automotives": [
            {
                "SparePartID": "51",
                "Name": "Brakes",
                "Price": "234",
                "VendorID": "70",
                "CompanyName": "FiveStar Automotives",
                 "Qty": 1,
                 "TotalPrice": "234"
            },
            {
                "SparePartID": "53",
                "Name": "Clutch",
                "Price": "999",
                "VendorID": "70",
                "CompanyName": "FiveStar Automotives",
                 "Qty": 1,
                 "TotalPrice": "999"
            },
              {
                "SparePartID": "55",
                "Name": "LED",
                "Price": "288",
                "VendorID": "70",
                "CompanyName": "FiveStar Automotives",
                 "Qty": 1,
                 "TotalPrice": "288"
            }
        ]
    
}

现在,我想为每个公司分别下订单。就像公司A:“ Master Automotives” 有2种产品,我想调用API分别

下订单

,然后再次调用B公司的API:“修复解决方案” ,该产品有1种产品并下了订单。对于mycart JSON对象中存在的所有公司,依此类推。

我想根据公司名称遍历 mycart 对象,并在同一功能中分别为每个公司 调用API。 (此对象 mycart 可以有多个公司。动态)。

这就是我的API调用方式。 我只是不知道如何在此函数中遍历我的JSON对象:

placeOrder()
{
      const orderData = {
      CustomerID: this.state.customer.CustomerID,
      TotalPrice: this.state.totalPrice } 
      //TotalPrice is each Company's items' total

    //nested API call
    axios.post('http://localhost/Auth/api/customers/create_order.php', orderData)
      .then((res) => {
      console.log(res.data);
      this.setState({orderID: res.data.oid});
   
      const cartOrder = this.state.mycart;
      alert("Congratulations! Your Order is Confirmed");
      return axios.post('http://localhost/Auth/api/customers/insert_order_details.php', cartOrder)
      .then((result) => {
          alert("Your all order items added to order details table");
      });
  });

}

1 个答案:

答案 0 :(得分:0)

var requestObj = {
    company1: [{
        name: "Prod1",
        qty: "12"
    }, {
        name: "Prod2",
        qty: "10"
    }],
    company2: [{
        name: "Prod3",
        qty: "11"
    }, {
        name: "Prod4",
        qty: "13"
    }],
    company3: [{
        name: "Prod5",
        qty: "1"
    }, {
        name: "Prod6",
        qty: "2"
    }, {
        name: "Prod7",
        qty: "3"
    }]
}

for(let companyName in requestObj){
    let orderData = requestObj[companyName];
    placeOrder(orderData);
}