Is there a way to do this in a single statement?
var {a:c, b} = {a:2, b:3}; // es6 destructuring
var d = {b, c} // es6 shorthand properties
I want to transform {a:2, b:3}
to {b:3, c:2}
in a single statement.
Don't use destructuring and shorthand properties, just construct your literal like you want:
var input = {a:2, b:3};
var d = {b:input.b, c:input.a}; // single statement
Alternatively use an immediately invoked arrow function (IIAF):
var d = (({a:c, b}) => ({b, c}))(input);
I want to transform {a:2, b:3} to {b:3, c:2} in a single statement.
You just need to swap the properties:
const {b, a:c} = {a:2, b:3}
b // 3
c // 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With