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.
CodePudding user response:
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);
CodePudding user response:
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)