I have an array of objects with the same keys, example:
const data = [{name: "firstName", value: "John"},
{name: "lastName", value: "Smith"},
{name: "Number", value: "421-8686"}
]
I however want the values to become the key and values. example:
const data = [{"firstName": "John"},
{"lastName": "Smith"},
{"Number": "421-8686"}]
I tried a few methods, but for some reason or another I fail. My latest attempt is:
let newData = {}
arrayData.forEach((item, index) => {
let key = data.name
let value = data.value
newData[index] = { key: value} // this results in {"key": "John"} --> but i want {"firstName": "John"}
});
You can use Object.fromEntries.
let data = [
{name: "firstName", value: "John"},
{name: "lastName", value: "Smith"},
{name: "Number", value: "421-8686"}
]
data = data.map(info => Object.fromEntries([[info.name, info.value]]))
console.log(data)