How can I iterate over a json without static main key?

I’m trying to iterate over a json object with multiple json objects inside, but the main key who allow me to access to the objects is not static , How can I iterate over those objects and at the same time to know the main key who allow me to have access to the object?

Thanks in advance

let res = {
     Zw== : { price: "500", code: "Zw==", name: "Word 2013", image: "path/d360f0.jpg", total: 1},
     aA== : { price: "300", code: "aA==", name: "Power point 2013", image: "path/80fa572.png", total: 1},
     xH== : { price: "300", code: "aA==", name: "Power point 2013", image: "path/80fa572.png", total: 1}
};

3 thoughts on “How can I iterate over a json without static main key?”

  1. Your object properties look broken, so I would look into that.

    I think what you’re looking for is Object.keys..

    Example:

    function printKeys(obj) {
        console.log(Object.keys(obj));
    }
    

    You can pass in your res object and it will give you have the property keys of the object.

    From there then you can use the property as the next key.

    printKeys(res['Zw']);

    JSFiddle

    Reply

Leave a Comment