如何将数组元素作为键分散到对象中,并为它们定义一些值?
假设我有一个数组:
const arr = ['a', 'b, 'c'];
我想创建一个像这样的对象:
{ 'a': true, 'b': true, 'c': true}
我该怎么做?
const obj = {...arr: true}
没用
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用:
Using
Array#reduce
:是的,这不是一个有效的任务。想到的第一种方法 - 如果您想要那些酷的ES6单线 - 是:
Yeah, that's not a valid assignment. The first way that comes to mind - if you want one of those cool ES6 one-liners - would be:
使用
object.assign
并传播数组(带有映射)。Using
Object.assign
and spread the array (with map).