Matching A Computed Property In Function Args
The computed property name feature of ES6 allows you to reference a variable in object assignments and destructurings. This syntax is flexible enough that it can be used in the arguments portion of a function declaration. In fact, it can even be matched against another argument -- allowing the creation of some handy, yet terse functions.
Notice that the first argument, key
, will match against the computed property name in the second argument. The foundValue
will correspond to whatever key
maps to in the given object.
This get
function can then be used like so.
h/t @sharifsbeat
PreviousMake The Browser Editable With Design ModeNextMatching Multiple Values In A Switch Statement
Last updated