0

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

6
  • check Object.entries. Commented Jan 3, 2023 at 10:05
  • same result @NajamUsSaqib not as i expected.. Commented Jan 3, 2023 at 10:10
  • 2
    Is there more to your attempt? Commented Jan 3, 2023 at 10:14
  • yes i try more my mapping out specific key but still cannot reach the result i want Commented Jan 3, 2023 at 10:15
  • Can you explain what you're trying to do, what logic you're trying to apply to process the Objects in the Array? What are you changing, and why? Commented Jan 3, 2023 at 10:19

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.