gpt4 book ai didi

javascript - 将数组转换为对象键

转载 作者:行者123 更新时间:2023-12-01 14:50:20 28 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Return object with default values from array in Javascript

(4 个回答)



How to convert an Object {} to an Array [] of key-value pairs in JavaScript

(19 个回答)


3年前关闭。




将数组转换为以这些数组值作为键的对象的最佳方法是什么,空字符串作为新对象的值。

['a','b','c']

至:
{
a: '',
b: '',
c: ''
}

最佳答案

试试 Array#Reduce

const arr = ['a','b','c'];
const res = arr.reduce((acc,curr)=> (acc[curr]='',acc),{});
console.log(res)

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

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