A Real-World Example
I had a list of users in my trusty Firebase database, and I was trying to implement a "following" property on each user object. This following node would actually contain a list of key-value pairs where the value is 1 and the key is the unique id (generated by Firebase when the user first signs up) of the user you want to follow. I was having trouble creating a new object with one key/value property whose key was set dynamically (I was having trouble, that is, until I remembered about the bracket notation).
Ok, so here's the situation. I have a user logged in through Firebase, and now I have his or her unique identifier (uid) saved into the variable loggedInUser.
Suppose, now, that I want to create a new object with a parameter whose key is equal to the value of the variable named loggedInUser. Naively creating the object like this:
just results in the new object having a key of the exact text, "loggedInUser". This is not what we wanted. We wanted the VALUE of loggedInUser to be the key; in other words the actual uid.
Luckily, we can create an object and then use bracket notation to set a parameter. It would look something like this:
The Awesomeness of Firebase
The posts on this site are written and maintained by Jim Lynch. About Jim...