Destructuring can be used to extract data with arrays or objects, in a declarative way, and is self documenting, allowing the next developer to read, to understand a lot about the intent of this line of code.
It is particularly useful to extract data from an array, because the destructing pattern reveals the shape of the data, and the name of the variables used in the pattern show what is the important data we want to extract from this array, or multidimensional array.
Note that it is possible to chain multiple destructring patterns, since each of them returns the original array.
When destructuring objects, the destructuring pattern uses curly braces, with the source property name on the left, and target variable on the right.
In the case the two are the same, in ES6, they don't need to be repeated.
It is also possible to destructure nested objects, in this case a nested destructuring pattern would be provided.
It is particularly useful to extract data from an array, because the destructing pattern reveals the shape of the data, and the name of the variables used in the pattern show what is the important data we want to extract from this array, or multidimensional array.
Note that it is possible to chain multiple destructring patterns, since each of them returns the original array.
When destructuring objects, the destructuring pattern uses curly braces, with the source property name on the left, and target variable on the right.
In the case the two are the same, in ES6, they don't need to be repeated.
It is also possible to destructure nested objects, in this case a nested destructuring pattern would be provided.
Comments
Post a Comment