Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create objects from main key and value from nested object

I’ll keep it simple. Suppose I have an object like this:

  let myObj = {
    name:{
      value: "John",
      type: "contains"  
    },
    age:{
      value: "5",
      type: "contains"  
    }
  }

how can I create a new object that contains the main key but the value is just the value of its nested object, as follows:

  let myNewObj = {
    name: "John",
    age: "5"
  }

Thanks in advance.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you just want to extract the value key for each object, you can do something like this:

let myObj = {
  name:{
    value: "John",
    type: "contains"  
  },
  age:{
    value: "5",
    type: "contains"  
  }
}
  

let newObj = {}

for (const key in myObj) {
    newObj[key] = myObj[key].value;
}

console.log(newObj);
// {
//   age: "5",
//   name: "John"
// }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading