My objective is to assign the variables to new object while submitting the data.
Here is the array of object as they appear while submitting:
experience: [
{
Organisation: "",
ID: "",
From: "",
To: "",
Skills: []
},
{
Organisation: "",
ID: "",
From: "",
To: "",
Skills: []
}
]
But i want the object should be in this way:
{
organisationName: "",
id: "",
from: "",
to: "",
skills: []
}
I have done in this way but output is not appearing in this appropriately.
handleSubmit = (e)=> {
e.preventDefault();
let obj = {
id: this.state.experience.map((item) => item.ID),
organisation: this.state.experience.map((item) => item.Organisation),
from: this.state.experience.map((item) => item.From),
to: this.state.experience.map((item) => item.To),
}
console.log("Object", obj)
}
Can anyone please help me to assign the values in a right way?
It would be easier to use Object.fromEntries
to map the entries and call toLowerCase
on the keys:
const lowercaseKeys = obj => Object.fromEntries(
Object.entries(obj)
.map(([key, val]) => [key.toLowerCase(), val])
);
const obj = this.state.experience.map(lowercaseKeys);
If you also need to replace a particular key with another, use .replace
:
const lowercaseKeys = obj => Object.fromEntries(
Object.entries(obj)
.map(([key, val]) => [
key.toLowerCase().replace('organisation', 'organisationName'),
val
])
);
const obj = this.state.experience.map(lowercaseKeys);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments