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

70
Views
How to move object properties inside an array of objects

I have an array of objects like this:

currentArray = [
  {
    year: 2011,
    asset: {
      silver: 322,
      gold: 325,
    },
  },
  {
    year: 2012,
    asset: {
      silver: 411,
      gold: 2235,
    },
  },
];

What needs to be done in JavaScript/TypeScript, to change it from the current structure to the structure below? The silver- and gold-property of each asset-object should be stored in the object above, next to 'year', and the asset-objects need to be deleted:

desiredArray = [
  {
    year: 2011,
    silver: 322,
    gold: 325,
  },
  {
    year: 2012,
    silver: 411,
    gold: 2235,
  },
];
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

let currentArray = [{
    year: 2011,
    asset: {
      silver: 322,
      gold: 325,
    },
  },
  {
    year: 2012,
    asset: {
      silver: 411,
      gold: 2235,
    },
  },
];

currentArray.map(item => {
  Object.assign(item, item.asset)
  delete item.asset;
  
  return item
})

console.log(currentArray)

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!