I'm looking for a way in javascript to transform this json to something else.
{
"John Doe": {
"place": "Amsterdam"
},
"Jane Doe": {
"place": "Paris"
}
}
To something like this:
{
{ "id": 0,
"name": "John Doe",
"place": "Amsterdam"
},
{ "id": 1,
"name": "Jane Doe",
"place": "Paris"
},
}
How can I achieve this with javascript?
You can use Object.entries
to get key/value
pair and use map
method to transform the object into the new one.
const data = {
"John Doe": {
"place": "Amsterdam"
},
"Jane Doe": {
"place": "Paris"
}
}
const result = Object.entries(data).map(([key, value], index) => ({
id: index,
name: key,
place: value.place
}))
console.log(result)