Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

0

Convert the object to its entries array and map through it to return [key,value]
Then convert to a new object using Object.fromEntries

  let myObj = {
    name:{
      value: "John",
      type: "contains"  
    },
    age:{
      value: "5",
      type: "contains"  
    }
  }
  
  let myNewObj = Object.fromEntries(Object.entries(myObj).map(([key,{value}])=>[key,value]))
  console.log(myNewObj)

about 4 years ago · Juan Pablo Isaza Report

0

In general, to be able to transform all the values of an object's properties according to a mapping function, you can use Object.entries to make an array of [key, value] arrays for each property, and then use Object.fromEntries to reconstitute an object from those arrays. You can provide a generic transformation function in the middle of that operation like this:

const transformValues = (obj, transform) =>
  Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, transform(value)]));

The transform function you need in your specific case will take the property value (which is an object) and just return its value property. Like this: ({ value }) => value (This is pretty easy with destructuring.)

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

const transformValues = (obj, transform) =>
  Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, transform(value)]));

const result = transformValues(myObj, ({ value }) => value);

console.log(result);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!