javascript - 如何在 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)
推荐阅读
- javascript - 如果没有子列表,如何扩展列表?
- javascript - Javascript 不会影响我的烧瓶引导网站中的导航栏
- python - 有条件地增加熊猫
- docker - DockerCompose 构建一个镜像并运行多个容器
- python - 强制 ipython 以科学计数法输出
- azure - Power Bi URL 过滤器始终显示所有
- svgo - svg-sprite-loader outputPath 中断 svgo
- google-cloud-firestore - Firestore 问题 Saveall 方法
- python-3.x - 如何在数据框中查询复合词?
- excel - Excel VBA:图片的神秘移位