I would like to change key names in such object:
const data = {
key1: 'value1',
key2: 'value2',
key3: 'value3',
key4: 'value4',
}
with those keys from map:
const map = {
'key1': 'change1',
'key4': 'change4'
}
Using code below:
const replacedKeysInData = Object.keys(data).map((key) => {
const keyFromMap = map[key] || key;
return { [keyFromMap]: data[key] }
})
console.log(replacedKeysInData);
I get:
[
{ change1: 'value1' },
{ key2: 'value2' },
{ key3: 'value3' },
{ change4: 'value4' }
]
The point is that I would like to have it in one object:
{
change1: 'value1',
key2: 'value2',
key3: 'value3',
change4: 'value4'
}
I suppose it is simple, how can I do it?
Thanks