0

I've seen this in a repository, but I'm not quite sure what is exactly happening. Is the var value replaced or is the child question of value taken from var data?

const { value: question } = data;
const { value } = data;

  • It's just renaming. `{ value: question } = data` takes the property `value` and assigns it to a local variable called `question`. The second one takes the property `value` and assigns it to a local variable called `value`. – VLAZ Nov 01 '19 at 13:49

2 Answers2

2

Destructuring properties of an object

const { value } = data;

creates a block-scoped constant named value, and assigns to it data.value.

It's identical to

const value = data.value;

Destructuring properties of an object under a different variable name

const { value: question } = data;

creates a block-scoped constant named question, and assigns to it data.value.

It's identical to

const question = data.value;
connexo
  • 53,704
  • 14
  • 91
  • 128
0

A property can be unpacked from an object and assigned to a variable with a different name than the object property.

var o = {p: 42, q: true};
var {p: foo, q: bar} = o;

console.log(foo); // 42 
console.log(bar); // true

Here, for example, var {p: foo} = o takes from the object o the property named p and assigns it to a local variable named foo.

MDN - Destructuring assignment

Jurrian
  • 850
  • 6
  • 20