I'm wondering how I can transform an each item in an array into a specified object. You can see the code below for the array I start out with and the result I'm trying to achieve. I'm trying to use the map
function to no avail, and not sure if the array.map()
function is the right function to use, or if maybe there is something in lodash that I could use. Thanks!
const x = ["a", "b", "c"];
// expected result
{
"a": {"foo": "bar"},
"b": {"foo": "bar"},
"c": {"foo": "bar"},
}
You could map the new objects with the wanted key and assign to a single object.
const
x = ["a", "b", "c"],
object = Object.assign(...x.map(k => ({ [k]: { foo: "bar" } })));
console.log(object);