I have a json object made like this (array of object with array element)
var orders = [
{
"employ": {
"id": "1",
"name": "1_name",
},
"order": [
{
"number": "1_1",
"desc": "1_1_desc"
},
{
"number": "1_2",
"desc": "1_2_desc"
}
]
},
{
"employ": {
"id": "2",
"name": "2_name",
},
"order": [
{
"number": "2_1",
"desc": "2_1_desc"
}
]
}
]
and I have to denormalize it to get this, a simple array of objects
[
{
"emp_id": "1",
"emp_name": "1_name",
"ord_number": "1_1",
"ord_desc": "1_1_desc"
},
{
"emp_id": "1",
"emp_name": "1_name",
"ord_number": "1_2",
"ord_desc": "1_2_desc"
},
{
"emp_id": "2",
"emp_name": "2_name",
"ord_number": "2_1",
"ord_desc": "2_1_desc"
}
]
I use two nested for, but I was wondering if it is possible to do it in another way, perhaps more performing.
var orders_flat = [];
for(var i = 0; i < orders.length; i++) {
for(var a = 0; a < orders[i].order.length; a++) {
var o = {};
o.emp_id = orders[i].employ.id;
o.emp_name = orders[i].employ.name;
o.ord_number = orders[i].order[a].number;
o.ord_desc = orders[i].order[a].desc;
orders_flat.push(o);
}
}
TIA