Changing rest output in JSON format in which the keys are present in snake case and I need to convert it to camel case
I am developing a React Web app in which I call my API endpoint and it gives me response like this
[
{
"id": 1,
"goals_for": 0,
"goals_against": 0,
"points": 0
}
]
So here I want to convert the Keys of my JSON rest output from snake case to camel case like this ( goals_for
-> goalsFor
)
After conversion I need the output like this
[
{
"id": 1,
"goalsFor": 0,
"goalsAgainst": 0,
"points": 0
}
]
const snakeCaseArray = [{
"id": 1,
"goals_for": 0,
"goals_against": 0,
"points": 0
}]
/*using lodash*/
const camelCaseArray = snakeCaseArray.map(item=>{
return Object.keys(item).map(key=>{
const newKeyName = _.camelCase(key) /*---> lodash function*/
return {[newKeyName]:item[key]}
})
You need to take the entire key value pairs and pass all the keys to the function and create a new object in javascript by using this snake to camel case function
function snakeToCamel(your_Key){
return your_Key.replace(/(\-\w)/g, function(m){return m[1].toUpperCase();});
}
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.