Table of contents
No headings in the article.
Different ways in array destructuring:
You can skip items you don't want:
When you want to assign only few variables and rest as separate:
You can also set default values:
assign variables via destructering:
const DAYS = {
yesterday: 0,
today: 1,
tomorrow: 2
};
const {today: todayCount, tomorrow: tomorrowsCount } = DAYS;
console.log(todayCount);
> 1
console.log(tomorrowsCount);
> 2
assign variables for nested objects:
const DAYS = {
yesterday: { low: 0, high: 1 },
today: { low: 1, high: 2 },
tomorrow: { low: 2, high: 3 }
};
const {today: {low: lowToday, high: highToday }} = DAYS;
console.log(lowToday);
> 1
console.log(highToday);
> 2
Swap using destructuring:
We can also destructure nested ones like:
We can also achieve multi array destrcuturing: