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.

>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"
// }

Leave a Reply