这是对象:
{
"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"}
};
这是我的尝试,我不能通过重复$.each function
来获取part_name或part_state。
$.each( orders, function ( i, order ) {
console.log( "ORDER ID: " + i + "->" + order.order_name );
$.each( order, function ( j, part ) {
console.log( part );
});
});
答案 0 :(得分:3)
part
是一个对象数组。您可以使用的是:
$.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);
});
});