【发布时间】:2020-04-15 07:41:08
【问题描述】:
如何将["one","two","three"] 转换为{one:"one", two:"two", three:"three"}
import stringArray from './a.js';
class b {
hashmap = stringArray// convert this into Object here inline.
}
在你开始之前,我知道如何在说 constructor() 中使用 forEach、for in 循环等技巧来实现这一点。有没有简单的一行代码在类属性中而不是在函数内部实现这一点。
【问题讨论】:
-
stringArray.reduce((a, c) => (a[c] = c, a), {}) -
您也可以使用IIFE,例如:
hashmap = (() => {.. return "something"})()
标签: javascript typescript lodash