I'm parsing the following from:
json.listMap.forEach((listItem: { State: string; Country: string})```
Response.data looks like this:
```{
success: true,
listMap: [
{
State : 'Northrine Westfalia',
Country: 'Germany'
},
{
{
State : 'Bavaria',
Country: 'Germany'
}
}
]
}
How can I convert the value in country currently being 'Germany' to 'Deutschland' after parsing it from the API using Java Script?
-> Let me be more open here. I don't really know that method I could use to do this. Maybe someone could point me at a documentation, of course I don't expect anyone to just hand me the code.
Use Array.map after parsing JSON into an JavaScript array:
const list = response.listMap.map(entry => {
return {
State : entry.State,
Country: entry.Country.replaceAll('Germany', 'Deutschland')
}
})
Of course, if you need JSON structure, just convert final array into JSON string with the following:
const newJSONList = JSON.stringify(list);
try this :
listMap = listMap.map(listMapItem => {
if ('Germany'===listMapItem.Country) {
listMapItem.Country = 'Deutschland';
return listMapItem;
} else {
return listMapItem;
}
});
assuming below is your response object.
var responsObj={ success: true,
listMap: [
{
State : 'Northrine Westfalia',
Country: 'Germany'
},
{
State : 'Bavaria',
Country: 'Germany'
}
]
};
responsObj.listMap=responsObj.listMap.map(x=>{
if(x.Country=="Germany"){
x.Country="Deutschland";
}
return x;
});
console.log(responsObj.listMap);