将数组转换为对象布尔值 [英] Convert an array into object boolean
本文介绍了将数组转换为对象布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个数组 [一个,两个,三个]
想要将其转换成这个 {one:true ,两个:true,三个:true}
。基本上,所做的就是将数组项转换为对象的键,并使它们为布尔值。我试过使用价差运算符 {... arr}
,结果为 {0:'one',1:'two',2:'三个'}
,有任何想法吗?
I have this array ["one","two","three"]
would like to convert it into this {one:true, two:true, three:true}
. Basically what am doing is converting the array items to keys of an object and make them boolean. I have tried using spread operator {... arr}
which results to {0:'one',1:'two',2:'three'}
, Any Ideas?
推荐答案
您可以映射对象并将它们分配给单个对象。
You could map objects and assign them to a single object.
var array = ["one", "two", "three"],
object = Object.assign(...array.map(k => ({ [k]: true })));
console.log(object);
这篇关于将数组转换为对象布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文