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

121
Views
JavaScript objects key manipulation issue

I want to make this object:

    let data = {
        "car": {
            "model": 1999
        },
        "van": {
            "model": 1850
        }
    }

to look like this:

let data = {
    "car" : 1999,
    "van": 1850
}

the goal is to remove the key model and keep its value.

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

0

let data = {
    "car": {
        "model": 1999
    },
    "van": {
        "model": 1850
    }
}

const dataFormated = Object.entries(data).reduce((acc, [k, {model}]) => ({...acc, [k] : model}), {})
about 4 years ago · Juan Pablo Isaza Report

0

You can do using this approach

let data = {
    "car": {
        "model": 1999
    },
    "van": {
        "model": 1850
    }
}

let result = Object.keys(data).map(function(e){
    return {[e] : data[e]['model']};
});
about 4 years ago · Juan Pablo Isaza Report

0

You can grab the Object.entriesfrom the data object (key/value pairs in an array), and then iterate over them to update a new object.

const data={car:{model:1999},van:{model:1850}};

// Initialise a new object
const out = {};

// Get the entries from the data
// Each entry will be an array  with
// a key/value pair:
// ["car", { "model": 1999 }]
const entries = Object.entries(data);

// Then for each of those arrays destructure
// the key and obj, and update your new object
// using that information
for (const [key, obj] of entries) {
  out[key] = obj.model;
}

// Ta da!
console.log(out);

Additional documentation

  • Loops and iteration

  • Destructuring assignment

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!