[英]Restructure JavaScript object into array of objects with named keys
[英]Restructure Array of object javascript
所以我有一個結構為 object 的數組:
const data= [
{
id: '6397f6f46b18bc89cb37053c',
cost_center: null,
plant: null,
material: null
},
{
id: '6397f7166b18bc89cb372ff7',
cost_center: 'C118as0008',
short_description: 'LINE PasdNG ALL',
plant: 'K1as8',
material: '300006',
material_name: 'DRILLO PBJ 22g MT (12 pcs x 8 ib)',
base_quantity: 218.995,
ZAP_DP: { acttyp: 'ZAP_DP', stdval: 0.224, unit: 'HR' },
ZAP_EL: { acttyp: 'ZAP_EL', stdval: 0.224, unit: 'HR' },
ZAP_LH: { acttyp: 'ZAP_LH', stdval: 0.224, unit: 'HR' },
ZAP_OT: { acttyp: 'ZAP_OT', stdval: 0.224, unit: 'HR' },
kwh: 35.46,
no_mc_mmbtu: 5,
noempl: 50
},
{
id: '6397f7166b18bc89cb373003',
cost_center: 'C11asd9',
short_description: 'LINE WAasdAT FOX',
plant: 'K1aas8',
material: '300007',
material_name: 'asBAasd0g GT (60 pcs)',
base_quantity: 28.816,
ZAP_DP: { acttyp: 'ZAP_DP', stdval: 0.33, unit: 'HR' },
ZAP_EL: { acttyp: 'ZAP_EL', stdval: 0.33, unit: 'HR' },
ZAP_GS: { acttyp: 'ZAP_GS', stdval: 0.33, unit: 'HR' },
ZAP_LH: { acttyp: 'ZAP_LH', stdval: 0.33, unit: 'HR' },
ZAP_OT: { acttyp: 'ZAP_OT', stdval: 0.33, unit: 'HR' },
kwh: 72.67,
no_mc_mmbtu: 1.85,
noempl: 14.5
},
]
我嘗試重組它,將 ZAP_EL 等數據映射到我喜歡的數組中。
[
{
id: '6397f6f46b18bc89cb37053c',
cost_center: null,
plant: null,
material: null
},
{
id: '6397f7166b18bc89cb372ff7',
cost_center: 'C118as0008',
short_description: 'LINE PasdNG ALL',
plant: 'K1as8',
material: '300006',
material_name: 'DRILLO PBJ 22g MT (12 pcs x 8 ib)',
base_quantity: 218.995,
ZAP_DP_acttyp: 'ZAP_DP', ZAP_DP_stdval: 0.33, ZAP_DP_unit: 'HR' ,
ZAP_EL_acttyp: 'ZAP_EL', ZAP_EL_stdval: 0.33, ZAP_EL_unit: 'HR' ,
ZAP_GS_acttyp: 'ZAP_GS', ZAP_GS_stdval: 0.33, ZAP_GS_unit: 'HR' ,
ZAP_LH_acttyp: 'ZAP_LH', ZAP_LH_stdval: 0.33, ZAP_LH_unit: 'HR' ,
ZAP_OT_acttyp: 'ZAP_OT', ZAP_OT_stdval: 0.33, ZAP_OT_unit: 'HR' ,
kwh: 35.46,
no_mc_mmbtu: 5,
noempl: 50
},
{
id: '6397f7166b18bc89cb373003',
cost_center: 'C11asd9',
short_description: 'LINE WAasdAT FOX',
plant: 'K1aas8',
material: '300007',
material_name: 'asBAasd0g GT (60 pcs)',
base_quantity: 28.816,
ZAP_DP_acttyp: 'ZAP_DP', ZAP_DP_stdval: 0.33, ZAP_DP_unit: 'HR' ,
ZAP_EL_acttyp: 'ZAP_EL', ZAP_EL_stdval: 0.33, ZAP_EL_unit: 'HR' ,
ZAP_GS_acttyp: 'ZAP_GS', ZAP_GS_stdval: 0.33, ZAP_GS_unit: 'HR' ,
ZAP_LH_acttyp: 'ZAP_LH', ZAP_LH_stdval: 0.33, ZAP_LH_unit: 'HR' ,
ZAP_OT_acttyp: 'ZAP_OT', ZAP_OT_stdval: 0.33, ZAP_OT_unit: 'HR' ,
kwh: 72.67,
no_mc_mmbtu: 1.85,
noempl: 14.5
},
]
有可能像這樣重組它嗎? 這是我的嘗試,但我無法得到我想要的結果:
const transformed = data.map((el)=>{
for (const property in el) {
for (const prop in property){
}
console.log(`${property}:${el[property]}`);
}
})
有什么幫助嗎? 或者有人可以指出我在這里做錯了什么......
你可以使用 Object.entries 和 flatMap 來做到這一點
像這樣
const transform = data => data.map(el => Object.fromEntries( Object.entries(el).flatMap( ([key, val]) => val && typeof val === 'object'? Object.entries(val).map(([k, v]) => [key + '_' + k, v]): [[key, val]] ) ) ) const data = [{ id: '6397f6f46b18bc89cb37053c', cost_center: null, plant: null, material: null }, { id: '6397f7166b18bc89cb372ff7', cost_center: 'C118as0008', short_description: 'LINE PasdNG ALL', plant: 'K1as8', material: '300006', material_name: 'DRILLO PBJ 22g MT (12 pcs x 8 ib)', base_quantity: 218.995, ZAP_DP: { acttyp: 'ZAP_DP', stdval: 0.224, unit: 'HR' }, ZAP_EL: { acttyp: 'ZAP_EL', stdval: 0.224, unit: 'HR' }, ZAP_LH: { acttyp: 'ZAP_LH', stdval: 0.224, unit: 'HR' }, ZAP_OT: { acttyp: 'ZAP_OT', stdval: 0.224, unit: 'HR' }, kwh: 35.46, no_mc_mmbtu: 5, noempl: 50 }, { id: '6397f7166b18bc89cb373003', cost_center: 'C11asd9', short_description: 'LINE WAasdAT FOX', plant: 'K1aas8', material: '300007', material_name: 'asBAasd0g GT (60 pcs)', base_quantity: 28.816, ZAP_DP: { acttyp: 'ZAP_DP', stdval: 0.33, unit: 'HR' }, ZAP_EL: { acttyp: 'ZAP_EL', stdval: 0.33, unit: 'HR' }, ZAP_GS: { acttyp: 'ZAP_GS', stdval: 0.33, unit: 'HR' }, ZAP_LH: { acttyp: 'ZAP_LH', stdval: 0.33, unit: 'HR' }, ZAP_OT: { acttyp: 'ZAP_OT', stdval: 0.33, unit: 'HR' }, kwh: 72.67, no_mc_mmbtu: 1.85, noempl: 14.5 } ] console.log(transform(data))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.