如何在 JavaScript ES6 中使用数组解构并分配给对象 属性
How to use array destructuring and assign to an object property in JavaScript ES6
此代码:
const eat = { when: 'now' }
const fruits = ['apple', 'orange']
eat.fruit = fruits[1] // orange
我可以像这样使用数组解构:
const [, selectedFruit] = fruits
eat.fruit = selectedFruit
但是我可以用它做一个单线吗?
你能用那个吗
[, eat.fruit] = fruits // remove const
console.log(eat)
您可以在这里使用合并,例如:
let eat = { when: 'now' }
const fruits = ['apple', 'orange']
eat = {...eat, fruit: fruits[1]}
console.log( eat )
此代码:
const eat = { when: 'now' }
const fruits = ['apple', 'orange']
eat.fruit = fruits[1] // orange
我可以像这样使用数组解构:
const [, selectedFruit] = fruits
eat.fruit = selectedFruit
但是我可以用它做一个单线吗?
你能用那个吗
[, eat.fruit] = fruits // remove const
console.log(eat)
您可以在这里使用合并,例如:
let eat = { when: 'now' }
const fruits = ['apple', 'orange']
eat = {...eat, fruit: fruits[1]}
console.log( eat )