Destructuring
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
Arrays
Assigning the values in the array to variables
let [a, b] = [1, 2];
console.log(a, b);
//=> 1 2Combine with spread/rest operator (accumulates the rest of the values)
let [a, ...b] = [1, 2, 3];
console.log(a, b);
// => 1 [ 2, 3 ]Omit values
const address = [221, 'Baker Street', 'London'];
const [ houseNo, , city ] = address;
console.log(houseNo, city)
// 221 'London'Swap values without a temp variable
var a = 1, b = 2;
[b, a] = [a, b];
console.log(a, b);
// => 2 1Objects
Selecting only some of the fields to destructure
Combines spread and destructuring
Advanced Usage
First understand this case
Now, think about how the above is being applied to the parameter passed to the map function.
In a for of loop
Resources
Last updated
Was this helpful?