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

95
Views
delete previous object fields and make new fields in array of objects in javascript

I have a list of objects in javascript where I have a list with 1 field which is org_name like this:

const list=[
{ org_name:"Test1"},
{ org_name:"Test2"}
]

Now I want to change this list to:

const list=[
{ text:"Test1",value:"Test1"},
{ text:"Test2",value:"Test2"}
]

What I am doing is:

list.map(el => ({ ...el, text: el.org_name, value: el.org_name }));

Its not working,Any leads will be appreciated.

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

0

Just drop the ...el part since you don't need it.

const list=[
  { org_name:"Test1"},
  { org_name:"Test2"}
];

const out = list.map(el => {
  return { text: el.org_name, value: el.org_name };
});

console.log(out);

about 4 years ago · Juan Pablo Isaza Report

0

Array.map creates new array. You want to update existing array, so loop through the array and update the object keys.

const list = [ { org_name: "Test1" }, { org_name: "Test2" } ];
list.forEach(el => {
  el.text = el.org_name;
  el.value = el.org_name;
  delete el.org_name;
});
console.log(list)

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!