arpit todewale
arpit todewale

Reputation: 31

Unable to set the JSON object in Array ionic

I am trying to run an Api to View my placed orders.

The Data is as follows:

{
"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": ""
    }
]

}

how to get only the object inside the main "orders" key

right now i am getting the data like:

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", …}

but i want to get data like:

[{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", …}]

my code to fetch the data is:

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);

        });
  });

Upvotes: 0

Views: 30

Answers (1)

Videsh Chauhan
Videsh Chauhan

Reputation: 371

Hi @arpit you can use the push function for this result. check it out my code

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

this will definitely help you.

Upvotes: 1

Related Questions