0

Here is the object:

{
    "358":  { "order_name": "NAME3", "parts": [
            { "part_name": "item1", "part_state": "add" },
            { "part_name": "item2", "part_state": "add" },
            { "part_name": "item3", "part_state": "remove" }
        ]
    },
    "359":  { "order_name": "NAME4"}
};

Here is my attempt, I cannot get the part_name nor part_state by repeating the $.each function.

$.each( orders, function ( i, order ) {
    console.log( "ORDER ID: " + i + "->" + order.order_name );

    $.each( order, function ( j, part ) {
        console.log( part );
    });
});
0

1 Answer 1

3

part is an array of object(s). What you can use is:

$.each(orders, function (i, order) {
    console.log("ORDER ID: " + i + "->" + order.order_name);
    if (!order.parts) return;
    $.each(order.parts, function (j, part) {
        console.log(part.part_name, part.part_state);    
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Of course, I don't know how I missed that. Thank you.

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.