I have an object like below
[
{
"day" : "monday",
"value": 1
},
{
"day" : "tuesday",
"value": 2
},
...
]
Is there any native javascript way to replace the key with a new key. Here I need to replace the "day" & "value" with "x" & "y" respectively. (like below)
[
{
"x" : "monday",
"y": 1
},
{
"x" : "tuesday",
"y": 2
},
...
]
you can use map and inside the call back create a new object and modify it as required. Then return this object
const data = [{
"day": "monday",
"value": 1
},
{
"day": "tuesday",
"value": 2
}
];
const newData = data.map((item) => {
const obj = {};
for (let keys in item) {
if (keys === 'day') {
obj['x'] = item[keys]
}
if (keys === 'value') {
obj['y'] = item[keys]
}
};
return obj
});
console.log(newData)