change property name

I have a JavaScript object as follows:

var a = { 
 Prop1: 'test', 
 Prop2: 'test2' 
}

How would I change the property name of Prop1 to Prop3?

I tried the following code but it didn't work...

for (var p in r){
  p.propertyName = 'Prop3';
}

That isn't directly possible.

You can just write

a.Prop3 = a.Prop1;
delete a.Prop1;

Another approach would be to use the proposed property rest notation like so:

const {Prop1, ...otherProps} = a;
    
const newObj = {Prop3: Prop1, ...otherProps};

This is supported by Babel's object rest spread transform.


Adding to the object rest spread solution

const { Prop1: Prop3, ...otherProps } = a;
const newObj = { Prop3, ...otherProps };