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.
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);
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.