I am trying to rename some of the properties on a series of objects in an array in order, but I only want to change the name to some of the properties and not having to rebuild the whole object to maintain the same order.
const data = [
{
prop1: 'Change key but keep position',
second: 'Keep same key name and position',
prop3: 'Change key but keep position',
fourth: 'Keep same key name and position',
}
]
const res = data.map(({prop1, prop3, ...obj}) => ({
first: prop1,
third: prop3,
...obj,
}))
console.log(res)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Desired output
[
{
first: 'Change key but keep position',
second: 'Keep same key name and position',
third: 'Change key but keep position',
fourth: 'Keep same key name and position'
}
]