我一直在尝试创建一个动态函数,该函数可以将嵌套的 key/val 对象展平在对象数组中
我如何获得这种结构?知道我想动态调用键并将父键和子键与_绑定。
输入:
const list = [
{
month: 'Jan',
value: 20,
metric: {
name: 'Rice',
measurement: 'Kg',
is_currency: false
},
entity: {
name: 'Rev',
type: 'limited company'
}
},
{
month: 'Jan',
value: 1000,
metric: {
name: 'Revenue',
measurement: 'Dollars',
is_currency: true
},
entity: {
name: 'Rev',
type: 'limited company'
}
},
]
预期输出:
const list = [
{
month: 'Jan',
value: 20,
metric_name: 'Rice',
metric_measurement: 'Kg',
metric_currency: false,
entity_name: 'Rev',
entity_type: 'limited company'
},
{
month: 'Jan',
value: 1000,
metric_name: 'Revenue',
metric_measurement: 'Dollars',
metric_is_currency: true
entity_name: 'Rev',
entity_type: 'limited company'
},
]