Matt Coady Matt Coady - 1 month ago 27
Javascript Question

Firebase, variable as key name

what I basically want to do is this:

variable = 'whatever';
fb.set({ variable : "More Stuff" });


So this will result in an entry that looks like:

whatever: "More Stuff"


Currently it just ends up as

variable: "More Stuff"


Can this be done?

Answer

Yes. The code is not working as expected because you are using object literal notation, which is the reason the it keeps the variable name as key, because that is how the notation works.

Solution

foo = {}; 
foo[variable] = 'more stuff'; 
fb.set(foo);