0

I write my data chronologically via push(), but I need to read it via the REST API, which returns the data as a obj instead of an array. That's a problem, since JS doesn't support ordered dictionaries.

What's the best way to get around that?

Jonathan K
  • 651
  • 6
  • 15

1 Answers1

1

Firebase does not store arrays when you use push() 1 2. Instead, it creates a unique, time-based id and stores the value in an object using that ID. They look something like this:

push ids stored in Firebase

There are a lot of reasons why this approach was chosen, and why it's a bad idea to utilize arrays in a real-time data store 1.

One nicety is that all modern browsers properly order objects in every case, excluding the highly contested numeric sorting of chrome 1 2 3.

Given these facts, and the fact that Firebase's push ids are strings that sort lexicographically, the REST API will return the records in the same order they are pushed, and as they appear in Forge, which a quick curl test will confirm.

The only place you could get into trouble would be by using numeric ids, which could naturally cause some mahem in Chrome. Of course, those are easily fixed by putting a "0" or a string prefix on each id to force Chrome to sort them lexicographically as well 1.

var a = {"foo":"bar", "03": "3", "02":"2", "01":"1"};
for(var i in a) { print(i) };

produces following output as expected:
foo
3
2
1
Community
  • 1
  • 1
Kato
  • 40,352
  • 6
  • 119
  • 149