Skip to content

Instantly share code, notes, and snippets.

@mvictorl
Last active August 3, 2020 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvictorl/acd9635ae59a0e441a9ac91ec384360a to your computer and use it in GitHub Desktop.
Save mvictorl/acd9635ae59a0e441a9ac91ec384360a to your computer and use it in GitHub Desktop.
Array from Object of objects (handling fetrch answer)
const respose = axios.get(`http://url.com/api/users`) // async/await required
const payload = Object.keys( respose.data || {} ).map( key => {
return {
...respose.data[key],
id: key,
}
})
/*
From object:
respose.data = {
'id123': {
name: 'John',
age: 23
},
'id231': {
name: 'Max',
age: 25
},
'id312': {
name: 'Tod',
age: 21
}
}
To Array:
payload = [{
id: 'id123'
name: 'John',
age: 23
},
{
id: 'id231'
name: 'Max',
age: 25
},
{
id: 'id312'
name: 'Tod',
age: 21
}]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment