It is hard to put into words, so If anyone could help me with the title of the question, thanks in advance. At the end of the day, I have an array like so;
[
{
field: "firstname",
value: "John"
},
{
field: "lastname",
value: "Doe"
},
{
field: "hobbies",
value: "singing, basketball"
},
]
and the desired output will be like below;
[
{
"firstname":"John"
},
{
"lastname":"Doe"
},
{
"hobbies" :"singing, basketball"
},
]
The closest question I could find similar to mine was this one: How to convert an array into an object in javascript with mapped key-value pairs?
Use map and return object, where key will be
obj.field
and value asobj.value
i.e.{[obj.field]: obj.value}
let output = [
{
field: "firstname",
value: "John"
},
{
field: "lastname",
value: "Doe"
},
{
field: "hobbies",
value: "singing, basketball"
},
].map(a => ({[a.field]: a.value}))
console.log(output)