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
React map function returning current object plus new properties

How do you create an new array, with same objects but with extra properties. Sample below creates a new array with a subset of properties. But I try to do is create some new properties on existing objects. So result will be objects from accounts.data, with an extra property called test.

var options = accounts.data.map((o) => ({
  label: o.name,
  value: o.id,
  number: o.accountNumber,
  test: o.name+o.id
})) 
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Try this

const accounts = {
  data: [{
      name: 'Test 1',
      id: 0,
      number: "123456-10",
      otherProperty: "Hello World!"
    },
    {
      name: 'Test 2',
      id: 1,
      number: "123456-11",
      otherProperty: "Hello World!!"
    },
  ]
};

const options = accounts.data.map((account) => ({ ...account,
  test: account.name + account.id
}));

console.log(options)

about 4 years ago · Juan Pablo Isaza Report

0

You can do that using Object.assign

const accounts = {
  data: [
    {
      name: 'Test 1',
      id: 0,
      number: "123456-10",
      otherProperty: "Hello World!"
    },
    {
      name: 'Test 2',
      id: 1,
      number: "123456-11",
      otherProperty: "Hello World!!"
    },
  ]
}


var options = accounts.data.map((o) => Object.assign({},o, {
  label: o.name,
  value: o.id,
  number: o.accountNumber,
  test: o.name+o.id
})) 

console.log(options);

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!