0

MY JSON is:

[
    {
        "id": "5d7d855c-7301-4b2f-9676-3cb758316b4c",
        "products": [
            {
                "id": 1,
                "price": 699.0,
                "quantity": 2,
                "color": "silver",
                "size": "128GB"
            }
        ],
        "contact": {
            "name": "5423654",
            "surname": "gdfsgsdf",
            "address": "gfdsg",
            "phone": "53454353",
            "city": "5234532",
            "country": "gfdsgfds"
        },
        "shippingPrice": 0.0,
        "discount": null,
        "totalPrice": 1398.0
    }
]

I am trying to loop in it with for loop in


class Orders {
    _orders;

    constructor() {
        this.init().then(() => this.renderOrders());
    }

    async init() {
        this._orders = await this.getOrders();
    }

    GET(url) {
        try {
            return axios.get(url)
        } catch (e) {
            return e;
        }
    }

    getOrders() {
        return new Promise(resolve => {
            this.GET('http://localhost:8080/orders/getOrders').then(response => {
                resolve(response.data);
            })
        })
    }

 renderOrders() {
        const template = document.getElementById("orders__template");
        const container = document.getElementById("orders");
        container.innerHTML = "";

        console.log(typeof this._orders);

for (let key in this._orders) {
            if (this._orders.hasOwnProperty(key)) {

                for (let productKey in this._orders[key].products) {
                    if (this._orders[key].products.hasOwnProperty(productKey)) {
                        console.log(this._orders[key].products[productKey].id); //getting the id
                    }
                }

                for (let discountKey in this._orders[key].discount) {
                    if (this._orders[key].discount.hasOwnProperty(discountKey)) {
                        console.log(this._orders[key].discount[discountKey].id); //undefined(even when it's not null, why?
                    }
                }

                for (let contactKey in this._orders[key].contact) {
                    if (this._orders[key].contact.hasOwnProperty(contactKey)) {
                        console.log(this._orders[key].contact[contactKey].name); //undefined too
                    }
                }

            }

        }
}

document.addEventListener("DOMContentLoaded", () => {
    new Orders();
});

However, i am getting only at first for loop result, other discount, or contact are undefined. i tried with other dummy data, i am getting the same results, so, what is the problem? However, i have other examples where it do work, the same structure, whats wrong?

6
  • Is this._orders the array or the object? Commented Apr 23, 2021 at 14:30
  • @evolutionxbox object Commented Apr 23, 2021 at 14:31
  • May you update the example to show a minimal reproducible example? At the moment it's not clear how to run the code to reproduce the error you see Commented Apr 23, 2021 at 14:31
  • @evolutionxbox , edited post, take a look now Commented Apr 23, 2021 at 14:34
  • 1
    Your products is an array of object whereas contact is an object. The last loop, loops over the properties (name, surname etc.), not over the array items. Commented Apr 23, 2021 at 14:37

1 Answer 1

1

in the orders object, property discount and contact are not arrays so you don't need to use for loop:

for (let key in order) {
  if (order.hasOwnProperty(key)) {
    for (let productKey in order[key].products) {
      if (order[key].products.hasOwnProperty(productKey)) {
        console.log(order[key].products[productKey].id); //getting the id
      }
    }

    if (order[key].discount) {
      console.log(order[key].discount.id);
    }

    if (order[key].contact) {
      console.log(order[key].contact.name);
    }
  }
}
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.