-2

I want to quickly assign {x:1,y:2} to {row,col}, i.e. let row = objXY.x, let col = objXY.y.

I know the destructuring assignment can do this

let {x,y} = objXY

But is there any way to do like this?

let {row,col} = objXY
gonnavis
  • 336
  • 1
  • 7
  • 16

1 Answers1

4

You can assign destructured properties to new variable names as described here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Assigning_to_new_variable_names

For example

const obj = {x: 1, y: 2};
const {x: row, y: col} = obj;
console.log(row); // 1
console.log(col); // 2
azundo
  • 5,902
  • 1
  • 14
  • 21