9

There is some destructuring going on here:

const { [a]: b } = this.props

But, what does [a]: b do: what does the brackets with colon do? In my case, a is supplied as one of the props with a string value.

Kristianmitk
  • 4,528
  • 5
  • 26
  • 46
Rui Nunes
  • 798
  • 1
  • 8
  • 15
  • Apparently, [destructuring and assigning new names](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Assigning_to_new_variable_names) can be used in combination with [computed property names](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#Computed_property_names). Interesting, i did not know. – ASDFGerte Apr 26 '18 at 15:59
  • It's a computed property name, and works just like in an object literal. – Bergi Apr 26 '18 at 16:00
  • nice, I didn't know you could do dynamic key name destructuring! – Andy Ray Apr 26 '18 at 16:00

3 Answers3

3

This ES6 destructuring syntax is very similar to the new "Enhanced object literals" for defining objects with variable property names, so I think it's useful to see that first:

Pre-ES6, if you wanted to assign a value to an object with a property name that was variable, you would need to write

var obj = {};
obj[variable] = value

That's because while both the dot-notation and the object literal notation require using the actual property name, the obj[prop] notation allowed you to have a variable name.

ES6 introduced the extended object literal syntax, which included the ability to write

var obj = { [variable]: value } 

The same syntax was incorporated in destructuring, which is what your question shows.

The basic destructuring allows assigning variables given literal property names:

First, assigning to a variable with the same name as a property already in the object (docs):

var person = {name: "Mary"};
var {name} = person;
/// name = "Mary"

Second, assigning a variable with a different name than the one already in the object (docs):

var person = {name: "Mary"};
var {name: myName} = person;
/// myName = "Mary"

(Side-note: if you recognize that, in the first example, var {name} = ... was just short-hand for var {name: name} = ..., you'll see that these two examples match more logically.)

But what if you don't know which property you want from person? Then you can use that same new computed-property-name object syntax from above (docs):

var person = {name: "Mary"};
var propName = getPropUserWantToKnowAbout(); // they type in "name"
var {[propName]: value} = person;
/// value = "Mary"
Sam Fen
  • 5,074
  • 5
  • 30
  • 56
  • OP's code is for assigning the variable `b`, to whatever `this.props[a]` is, NOT for creating a new object with a variable property name (which is another, separate feature of ES6). – TKoL Apr 26 '18 at 16:06
  • @TKol can you point me to "creating a new object with a variable property name" on some docs please? – Rui Nunes Apr 26 '18 at 16:30
  • I've expanded my answer, and explained why I put the new object syntax first, @RuiNunes see if this helps. – Sam Fen Apr 26 '18 at 20:38
2

[a] is a computed property name

...allows you to put an expression in brackets [], that will be computed and used as the property name


{ [a]: b } is a destructuring assignment with assigning to new variable names using a computed property name

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


Thus you end up with having a variable b in current scope that holds the value of this.props[a]

Example

this.props = {foo : 1, bar: 2};

let p1 = 'foo';
let p2 = 'bar';

let { [p1]: b } = this.props;

console.log(b); // 1

let { [p2]: c } = this.props;

console.log(c); // 2
Kristianmitk
  • 4,528
  • 5
  • 26
  • 46
0

An example

var props = {'dynamic': 2}
var dyn = 'dynamic'
const {[dyn]:a} = props
console.log(a);
// logs 2

Check out this page: https://gist.github.com/mikaelbr/9900818

In short, if dyn is a string, and props has a property with that string as the name, accessible by props[dyn], then {[dyn]:a} = props will assign props[dyn] to a

TKoL
  • 13,158
  • 3
  • 39
  • 73
  • You're welcome to explain the downvotes. I showed an example of it in action, and the code provided works as described. – TKoL Apr 26 '18 at 16:02
  • You might consider adding an explanation (in words) that describes what your code demonstrates. You've linked to an explanation, but links go bad. – devlin carnate Apr 26 '18 at 16:39