Created
March 2, 2018 21:28
-
-
Save NickFoden/490b0cd34aba084cf92c3ac85382635e to your computer and use it in GitHub Desktop.
Convert Firebase Response Object to an array
This file contains hidden or 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
// export const snapshotToArray = snapshot => { | |
// let returnArr = []; | |
// snapshot.forEach(childSnapshot => { | |
// let item = childSnapshot.val(); | |
// item.key = childSnapshot.key; | |
// returnArr.push(item); | |
// }); | |
// return returnArr; | |
// }; | |
// firebase | |
// .database() | |
// .ref("/posts") | |
// .on("value", function(snapshot) { | |
// console.log(snapshotToArray(snapshot)); | |
// }); | |
// via https://ilikekillnerds.com/2017/05/convert-firebase-database-snapshotcollection-array-javascript/ | |
//Another version | |
// let ResultArray = Object.entries(snapshot).map(e => Object.assign(e[1], { key: e[0] })); |
also check this package out. it basically does this trick https://www.npmjs.com/package/object-to-array-convert
yes this code do the same thing
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
also check this package out. it basically does this trick https://www.npmjs.com/package/object-to-array-convert