【发布时间】:2018-12-14 19:39:25
【问题描述】:
我想知道如何将数组中的每个项目转换为指定的对象。您可以在下面看到我开始使用的数组的代码以及我想要达到的结果。我试图使用map 函数无济于事,并且不确定array.map() 函数是否是正确使用的函数,或者lodash 中是否有我可以使用的东西。谢谢!
const x = ["a", "b", "c"];
// expected result
{
"a": {"foo": "bar"},
"b": {"foo": "bar"},
"c": {"foo": "bar"},
}
【问题讨论】:
-
什么是
{ foo: 'bar' }?它来自哪里? -
简单的
forEach有什么问题:let res = {}; x.forEach(e => res[e] = {'foo': 'bar'});
标签: javascript lodash