Home > front end >  Restructure Array of object javascript
Restructure Array of object javascript

Time:01-03

so i have an array of object with structure:

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
},
        ]

i try to restructure it my mapping out the data of ZAP_EL and etc into array i like this..

[
            {
  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
},
        ]

is that possible to restructure it like that? here is my try but i cannot get the result as i want:

const transformed = data.map((el)=>{
 for (const property in el) {
     for (const prop in property){
         
     }
  console.log(`${property}:${el[property]}`);
}
})

any help on this? or can someone pointing out where did i do wrong here...

CodePudding user response:

you can use Object.entries and flatMap to do that

like this

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))

  • Related