TIL: Object destructuring assignment in JavaScript.
Basic usage
const { one, two } = { zero: 0, one: 1, two: 2 }
one // 1
two // 2
Assign with default value
const { zero, three = 3 } = { zero: 0, one: 1, two: 2 }
zero // 0
three // 3
Assign another variable name
const { one: foo, two: bar } = { zero: 0, one: 1, two: 2 }
foo // 1
bar // 2
Assign another variable name with default value
const { three: t = 3 } = { zero: 0, one: 1, two: 2 }
console.log(t) // 3
Nested object
const {
one,
two: { number, ordinal: two }
} = {
one: { number: 1, ordinal: "first" },
two: { number: 2, ordinal: "second" }
}
one // { number: 1, ordinal: 'first' }
number // 2
two // 'second'