0

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

        });
  });
1
  • What you have currently vs what you want to achieve are the same. Try pasting what you want in a console and printing it out. Commented Nov 2, 2018 at 2:19

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.