Home > other >  object conversion between objects javascript
object conversion between objects javascript

Time:12-05

how to change object from

const ob = {
  "61ac2727bc40d842e43c8726": {
    nama: {
      value: "dianaasdasd",
    },
    username: {
      value: "diana",
    },
    email: {
      value: "[email protected]",
      error: false,
    },
    userlevel: {
      value: "karyawan",
    },
    password: {
      value: "************************************************************",
    },
    jenis_kelamin: {
      value: "Perempuan",
    },
    alamat: {
      value: "sasddad",
    },
  },
};  

remove "61ac2727bc40d842e43c8726", value and return object as follows how to convert to

this image

https://i.stack.imgur.com/6Qi1C.png

CodePudding user response:

You can generate your object like this:

const { '61ac2727bc40d842e43c8726': {...obj} } = ob;
let newObj = {};
for(let key in obj){
 newObj[key] = obj[key].value
}

CodePudding user response:

You can do it like this

const ob = {
  "61ac2727bc40d842e43c8726": {
    nama: {
      value: "dianaasdasd",
    },
    username: {
      value: "diana",
    },
    email: {
      value: "[email protected]",
      error: false,
    },
    userlevel: {
      value: "karyawan",
    },
    password: {
      value: "************************************************************",
    },
    jenis_kelamin: {
      value: "Perempuan",
    },
    alamat: {
      value: "sasddad",
    },
  },
};  


// remove 61ac2727bc40d842e43c8726
const obWithoutKey = Object.values(ob)[0];

// extract key values
const obExpected = Object.entries(obWithoutKey)
.reduce((acc, [key, val])=>{ 
  console.log(key, val)
 acc[key]=val.value 
 return acc;
  
}, {})

console.log(obExpected)
  • Related