无法在阵列离子中设置JSON对象

时间:2018-10-31 10:56:51

标签: json http ionic3

我正在尝试运行Api以查看我的下单。

数据如下:

{
"orders": [
    {
        "id": 2145,
        "order_number": "2145",
        "order_key": "wc_order_5bd937646c2c5",
        "created_at": "2018-10-31T05:02:28Z",
        "updated_at": "2018-10-31T05:02:28Z",
        "completed_at": "1970-01-01T00:00:00Z",
        "status": "processing",
        "currency": "USD",
        "total": "70.00",
        "subtotal": "70.00",
        "total_line_items_quantity": 2,
        "total_tax": "0.00",
        "total_shipping": "0.00",
        "cart_tax": "0.00",
        "shipping_tax": "0.00",
        "total_discount": "0.00",
        "shipping_methods": ""
    },
    {
        "id": 2144,
        "order_number": "2144",
        "order_key": "wc_order_5bd93747e48e1",
        "created_at": "2018-10-31T05:01:59Z",
        "updated_at": "2018-10-31T05:01:59Z",
        "completed_at": "1970-01-01T00:00:00Z",
        "status": "processing",
        "currency": "USD",
        "total": "70.00",
        "subtotal": "70.00",
        "total_line_items_quantity": 2,
        "total_tax": "0.00",
        "total_shipping": "0.00",
        "cart_tax": "0.00",
        "shipping_tax": "0.00",
        "total_discount": "0.00",
        "shipping_methods": ""
    }
]

}

如何仅在“订单”主键中获取对象

现在我正在获取如下数据:

0: {id: 2145, order_number: "2145", order_key: "wc_order_5bd937646c2c5", created_at: "2018-10-31T05:02:28Z", updated_at: "2018-10-31T05:02:28Z", …}
1: {id: 2144, order_number: "2144", order_key: "wc_order_5bd93747e48e1", created_at: "2018-10-31T05:01:59Z", updated_at: "2018-10-31T05:01:59Z", …}

但是我想获取像这样的数据:

[{id: 2145, order_number: "2145", order_key: "wc_order_5bd937646c2c5", created_at: "2018-10-31T05:02:28Z", updated_at: "2018-10-31T05:02:28Z", …},
{id: 2144, order_number: "2144", order_key: "wc_order_5bd93747e48e1", created_at: "2018-10-31T05:01:59Z", updated_at: "2018-10-31T05:01:59Z", …}]

我获取数据的代码是:

this.http.get(url).subscribe((data) => {
      this.orderArray = data.json().orders;
      console.log(this.orderArray);
      this.orderArray.forEach(element => {

        this.orderArray2 = Array.of(element);
        console.log(this.orderArray2);

        });
  });

1 个答案:

答案 0 :(得分:1)

您好,@ arpit,您可以使用push功能获得此结果。查看我的代码

let myArray:any = [];
for (let key in data.orders) {
 myArray.push(data.orders[key]);
}
console.log(myArray);

这肯定会对您有所帮助。