Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

Bracket Notation for JavaScript Objects

10/21/2016

0 Comments

 
The bracket notation way of setting JavaScript objects is a funny thing since it's really easy to forget about when you think of objects in terms of JSON, but the square bracket notation can be super useful in certain situations, particularly when you want to set an object's key to the value of a JavaScript variable.

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).

Objects

​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.
const currentProfileUser = profileDisplay.uid;
const loggedInUser = firebase.auth().currentUser.uid
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:
var obj = {
    loggedInUser:1
};
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:
var obj = {};
obj[loggedInUser] = 1;

The Awesomeness of Firebase

If you're curious, here's the JavaScript code for setting this object in Firebase. Remember, the Firebase database is just a super fast JSON object in the cloud so you can easily set more complex blocks of data that are just JavaScript objects.
var myRef = firebase.database()
    .ref('/users/' + self.auth.currentUser.uid + '/following/');

myRef.set(obj, function (error) {
    console.log('ok ' + error);
});
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Want FREE access to
    my daily stock tips 
    ​newsletters??
    Sign up here:

    - Triple Gainers
    - Rippers N' Dippers
    - Growingest Growers
    ​

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    February 2021
    January 2021
    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2021