将字符串数组转换为具有相同键/值的对象 [英] Convert string array into object with same key/value
本文介绍了将字符串数组转换为具有相同键/值的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将 [一个",两个",三个"]
转换为 {一个:一个",两个:两个",三个:三个"}
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))
这篇关于将字符串数组转换为具有相同键/值的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文