gpt4 book ai didi

javascript - 将数组转换为对象 bool 值

转载 作者:行者123 更新时间:2023-11-28 13:00:34 25 4
gpt4 key购买 nike

我有这个数组["one","two","three"]想把它转换成这个 {one:true, two:true, three:true} 。基本上我所做的是将数组项转换为对象的键并使它们成为 bool 值。我尝试过使用扩展运算符 {... arr}结果为 {0:'one',1:'two',2:'three'} ,有什么想法吗?

最佳答案

您可以映射对象并将它们分配给单个对象。

var array = ["one", "two", "three"],
object = Object.assign(...array.map(k => ({ [k]: true })));

console.log(object);

关于javascript - 将数组转换为对象 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51003654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com