0

I currently have an object stored in Firebase (firestore) that looks like this:

{0: "first", 1: "second", 2: "third"}

and it is saved as a field for a document. I get it with a .get("field") and save it to an array in my js file. However, it is being saved as an object like the one above, when what I want is something like:

["first", "second", "third"]

I have tried a bunch of things but it seems more complex than it should be to convert this object to the array I want. Any idea how to do this in js?

Noah Omdal
  • 89
  • 12
  • 8
    Take a look to [Object.values()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values) – Shidersz May 09 '19 at 16:48

1 Answers1

0

First get the keys and then map them into an array.

const obj = {0: "first", 1: "second", 2: "third"}

let arrayfromObj = Object.keys(obj).map( key => obj[key])

//["first", "second", "third"]
GifCo
  • 1,350
  • 7
  • 13
  • @Mark Meyer object.keys is better supported. Still no support for IE with object.values() which probably doesnt matter to most people. PS: they are both 1 line of code so not sure what you mean by "all that" lol – GifCo May 09 '19 at 17:02
  • Fair enough. Maybe I just need more coffee. – Mark May 09 '19 at 17:05