Home > OS >  Firebase turning arrays to objects with key as index and value as the array item
Firebase turning arrays to objects with key as index and value as the array item

Time:01-14

I am using firebase realtime database, and when I try and upload a json object that has an array as a value, it gets converted. Example: What I upload:

{
"obj": [var1,var2,var3]
}

Firebase turns that into:

{
"obj": {
   0: var1,
   1: var2,
   2: var3
   }
}

I have no idea why this happens and it really messes with my code. If there is a way to work around this that would be great.

CodePudding user response:

That's the expected behavior: arrays are turned into nodes with sequential numerical keys when you store them in the database, and nodes with (mostly) sequential numerical keys are turned into arrays when you load them.

So if you load the obj back into your application, it should show up as an array again. If that is not happening, show the code that you use to store and load the data.

Also see: Best Practices: Arrays in Firebase.

  • Related