|
| 1 | +# About |
| 2 | + |
| 3 | +Object [destructuring][mdn-object-destructuring] is a concise way of extracting properties from an object. |
| 4 | +Its syntax is similar to an [object literal][mdn-object-literal] expression, but on the left-hand side of the assignment instead of the right. |
| 5 | + |
| 6 | +```javascript |
| 7 | +const weather = { |
| 8 | + sun: '☀️', |
| 9 | + sun_behind_small_cloud: '🌤️', |
| 10 | + sun_behind_cloud: '⛅', |
| 11 | + sun_behind_large_cloud: '🌥️', |
| 12 | + sun_behind_rain_cloud: '🌦️', |
| 13 | + cloud: '☁️', |
| 14 | + cloud_with_rain: '🌧️', |
| 15 | + cloud_with_snow: '🌨️', |
| 16 | + cloud_with_lightning: '🌩️', |
| 17 | + cloud_with_lightning_and_rain: '⛈️', |
| 18 | +}; |
| 19 | + |
| 20 | +const { sun, cloud, cloud_with_lightning } = weather; |
| 21 | + |
| 22 | +sun; |
| 23 | +// => '☀️' |
| 24 | + |
| 25 | +cloud; |
| 26 | +// => '☁️' |
| 27 | + |
| 28 | +cloud_with_lightning; |
| 29 | +// => '🌩️' |
| 30 | +``` |
| 31 | + |
| 32 | +## Renaming in assignment |
| 33 | + |
| 34 | +The syntax can extract the properties by their key like `sun`, `cloud`, and `cloud_with_lightning`, but can also pick a different name for the variable: |
| 35 | + |
| 36 | +```javascript |
| 37 | +const { sun: sunny, cloud: cloudy, cloud_with_rain: rainy } = weather; |
| 38 | + |
| 39 | +typeof cloud_with_rain; |
| 40 | +// => 'undefined' |
| 41 | + |
| 42 | +typeof rainy; |
| 43 | +// => 'string' |
| 44 | + |
| 45 | +rainy; |
| 46 | +// => 🌧️ |
| 47 | +``` |
| 48 | + |
| 49 | +The assignment is also not required to use all the values. |
| 50 | + |
| 51 | +## Default values |
| 52 | + |
| 53 | +The object destructuring assignment can provide _default values_ in case there is none in the source object: |
| 54 | + |
| 55 | +```javascript |
| 56 | +const { sun = '🌞', tornado = '🌪️', cloud_with_snow: snowy = '❄️' } = weather; |
| 57 | + |
| 58 | +sun; |
| 59 | +// => '☀️' |
| 60 | + |
| 61 | +tornado; |
| 62 | +// => '🌪️' |
| 63 | + |
| 64 | +snowy; |
| 65 | +// => '🌨️' |
| 66 | +``` |
| 67 | + |
| 68 | +The following is observed: |
| 69 | + |
| 70 | +- `sun` has extracted from the object `weather` without replacing it as it is present in the object `weather`, |
| 71 | +- `tornado` does not exist in the object `weather`, so the default value was used, |
| 72 | +- `cloud_with_snow` was extracted as the variable `snowy`, without replacing it, as `cloud_with_snow` is present in the object `weather`. |
| 73 | + |
| 74 | +## Destructuring function parameters |
| 75 | + |
| 76 | +The `weather` object has a lot of properties. |
| 77 | +It is possible to directly extract one or multiple properties from this object when it's passed to a function: |
| 78 | + |
| 79 | +```javascript |
| 80 | +function weatherReport({ sun }) { |
| 81 | + console.log(sun); |
| 82 | +} |
| 83 | + |
| 84 | +weatherReport(sun); |
| 85 | +// => '☀️' |
| 86 | +``` |
| 87 | + |
| 88 | +## Destructuring `for of` iteration |
| 89 | + |
| 90 | +When iterating over an `array` (or other iterable), and the items are objects, it is possible to destructure inside the `for (...) of iterable` statement: |
| 91 | + |
| 92 | +```javascript |
| 93 | +const { sun: sunny, cloud: cloudy, cloud_with_rain: rainy } = weather; |
| 94 | + |
| 95 | +const prediction = [ |
| 96 | + { |
| 97 | + chance: 0.8, |
| 98 | + weather: sunny, |
| 99 | + description: 'There is a 80% chance it will remain sunny.', |
| 100 | + }, |
| 101 | + { |
| 102 | + chance: 0.15, |
| 103 | + weather: cloudy, |
| 104 | + description: |
| 105 | + 'There is a 15% chance the clouds will keep the sun from poking through.', |
| 106 | + }, |
| 107 | + { |
| 108 | + chance: 0.05, |
| 109 | + weather: rainy, |
| 110 | + description: 'There is a small chance of rain.', |
| 111 | + }, |
| 112 | +]; |
| 113 | + |
| 114 | +for (const { weather: symbol, description } of prediction) { |
| 115 | + console.log(`${symbol}: ${description}`); |
| 116 | +} |
| 117 | + |
| 118 | +// '☀️: There is a 80% chance it will remain sunny.' |
| 119 | +// '☁️: There is a 15% chance the clouds will keep the sun from poking through.' |
| 120 | +// '🌧️: There is a small chance of rain.' |
| 121 | +``` |
| 122 | + |
| 123 | +## Related concepts |
| 124 | + |
| 125 | +[concept:javascript/array-destructuring]() |
| 126 | +[concept:javascript/rest-and-spread]() |
| 127 | + |
| 128 | +[mdn-object-destructuring]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring#object_destructuring |
| 129 | +[mdn-object-literal]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer |
0 commit comments