重新构造对象javascript的数组

s3fp2yjn  于 2023-01-04  发布在  Java
关注(0)|答案(1)|浏览(103)

所以我有一个对象数组,其结构为:

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等数据Map到数组中,我喜欢这样。

[
            {
  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]}`);
}
})

在这方面有什么帮助吗?或者有没有人能指出我在这里做错了什么...

am46iovg

am46iovg1#

你可以使用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))

相关问题