[英]How to use array destructuring and assign to an object property in JavaScript ES6
This code:这段代码:
const eat = { when: 'now' }
const fruits = ['apple', 'orange']
eat.fruit = fruits[1] // orange
I can use array destructuring like this:我可以像这样使用数组解构:
const [, selectedFruit] = fruits
eat.fruit = selectedFruit
But can I make a one-liner out of it?但是我可以用它做一个单线吗?
Could you use that你能用那个吗
[, eat.fruit] = fruits // remove const
console.log(eat)
You can use merging here like:您可以在此处使用合并,例如:
let eat = { when: 'now' } const fruits = ['apple', 'orange'] eat = {...eat, fruit: fruits[1]} console.log( eat )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.