Skip to content

Instantly share code, notes, and snippets.

@NickFoden
Created March 2, 2018 21:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NickFoden/490b0cd34aba084cf92c3ac85382635e to your computer and use it in GitHub Desktop.
Save NickFoden/490b0cd34aba084cf92c3ac85382635e to your computer and use it in GitHub Desktop.
Convert Firebase Response Object to an array
// 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] }));
@just1and0
Copy link

also check this package out. it basically does this trick https://www.npmjs.com/package/object-to-array-convert

@gxanshu
Copy link

gxanshu commented May 23, 2021

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