This is quite obviously. No need stupid loops. Here you go:
var data = [
{
time: 1,
country:
[
{ country_code: 'US', order_count: 11 },
{ country_code: 'CN', order_count: 21 },
{ country_code: 'VN', order_count: 31 }
]
},
{
time: 2,
country:
[
{ country_code: 'US', order_count: 12 },
{ country_code: 'CN', order_count: 22 },
{ country_code: 'VN', order_count: 32 }
]
},
{
time: 3,
country:
[
{ country_code: 'US', order_count: 13 },
{ country_code: 'CN', order_count: 23 },
{ country_code: 'VN', order_count: 33 }
]
}
]
// suppose data[0] contains all country codes
const all_country_codes = (data) =>
data[0].country.map(c => c.country_code);
const get_order_count = (data, code) =>
data.country.filter(d => d.country_code == code)[0].order_count;
const get_all_order_counts = (data, code) =>
data.map(d => get_order_count(d, code))
const make_node = (data, code) =>
({ country_code:code, data:get_all_order_counts(data, code) })
const output = (data) =>
all_country_codes(data).map(code => make_node(data, code));
console.log(output(data));
Output:
[
{ country_code: 'US', data: [ 11, 12, 13 ] },
{ country_code: 'CN', data: [ 21, 22, 23 ] },
{ country_code: 'VN', data: [ 31, 32, 33 ] }
]
solved How can group multi data array to 1 array