将字符串数组转换为具有相同键/值的对象
问题描述:
如何将 [一个",两个",三个"]
转换为 {一个:一个",两个:两个",三个:三个"}
import stringArray from './a.js';
class b {
hashmap = stringArray// convert this into Object here inline.
}
在跳转之前,我知道如何使用诸如forEach,for in loop等之类的技巧在构造函数()中实现此目标.是否有简单的一行代码就可以在类属性中而不是在函数内部实现此目标.>
Before you jump I know of for how to achieve this in say constructor() with tricks like forEach, for in loop etc. Is there a simple one line code to achieve this in the class property not inside a function.
答
尝试一下:
const arr = ["one","two","three"]
let obj = {}
arr.forEach(item => {obj[item] = item})
document.write(JSON.stringify(obj))