Last active
August 3, 2020 06:22
-
-
Save mvictorl/acd9635ae59a0e441a9ac91ec384360a to your computer and use it in GitHub Desktop.
Array from Object of objects (handling fetrch answer)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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