set a variable as json key [duplicate] - javascript

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 5 years ago.
I wish to use a variable as json key in javascript
var chtid = "1234"
firebase.database().ref().set ({chtid :"hi"});
where chtid would be a variable
I tried this way but to no success
var chtid = "1234"
firebase.database().ref().set ({[chtid] :"hi"});
Any simple soulution please.

Put it on a variable first:
var obj = {};
var chtid = "1234"
obj[chtid] = "Hi";
firebase.database().ref().set (obj);
hope that helps

Related

Why doesn't JavaScript convert a string to an Object? [duplicate]

This question already has answers here:
How to check if object property exists with a variable holding the property name?
(11 answers)
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 5 years ago.
I'm attempting to run a list of strings through an object. When I do it individually it works, but when I pass it through as a string it doesn't work. How would I fix this?
// this doesn't work
var a = "IntegrationItem1";
var data = faq.a;
// but this works
var data = faq.IntegrationItem1;
What's causing the first example to not work? Is the variable data seeing it as faq."IntegrationItem1" instead of faq.IntegrationItem1?
You can access properties of the object using it's names:
var a = "IntegrationItem1";
var data = faq[a];
what you need is faq["IntegrationItem1"] => faq[a]

Mongoose: How to update value in array at index given from variable? [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 5 years ago.
options.votes is an array.
This code works:
Poll.findOneAndUpdate({title},{$inc:{'options.votes.0':1}}
I need to replace 0 for a variable.
This does not work:
const query = 'options.votes.'+variable
Poll.findOneAndUpdate({title},{$inc:{query:1}}
Thanks for help
It's just an object with a string key
var obj = {};
obj['options.votes.'+variable] = 1;
Poll.findOneAndUpdate({title},{$inc:obj})
or with ES2015 and dynamic keys
const query = 'options.votes.'+variable;
Poll.findOneAndUpdate({title},{$inc:{[query]:1}})

Name of a variable as string, how to get value? [duplicate]

This question already has answers here:
Convert string to variable name in JavaScript
(11 answers)
Closed 6 years ago.
A javascript newbie question:
Let's say, there is a variable defined as follows:
var var1 = "Something"
On the other hand, I generate such strings (simplified):
var nmbr = "1"
var varname = "var" + nmbr
Now I have to get the value of a variable with such a name (varname as string).
document.write([???varname???])
should write 'Something'.
How to do this?
console.log(variable_name);
will print the variable in the console.

Javascript associative array not working [duplicate]

This question already has answers here:
How do I create a dynamic key to be added to a JavaScript object variable [duplicate]
(2 answers)
Closed 8 years ago.
I have this code, but I want the belongsto var to be retrieved as the key using the var value and not the var name:
var belongsto = panel.attr('data-belongsto');
var panelid = panel.attr('id');
tabValue.push({belongsto:panelid}); console.log(tabValue);
this returns [{'belongsto':'12345'}]
As you can see the the script takes belongsto as the key name but I need it to take the content of the variable.
Any help is appreciated, thanks-
Create the object first
var obj = {};
obj[belongsto] = panelid;
tabValue.push(obj);

Using string as variable name in JS? [duplicate]

This question already has answers here:
Is there a way to access a javascript variable using a string that contains the name of the variable?
(4 answers)
How to use a string as a variable name in Javascript? [duplicate]
(3 answers)
parse a string as an object from data attribute [duplicate]
(1 answer)
Use dynamic variable names in JavaScript
(19 answers)
Closed 9 years ago.
How can I make the code that follows to work?
var x = 'name';
and then, to use the value inside x, as it was a variable, and set it, so that if i want it to be NAME, i'll have the result:
var name = 'NAME';
Is it possible ?
Not directly, no. But, you can assign to window, which assigns it as a globally accessible variable :
var name = 'abc';
window[name] = 'something';
alert(abc);
However, a much better solution is to use your own object to handle this:
var name = 'abc';
var my_object = {};
my_object[name] = 'something';
alert(my_object[name]);
I haven't seen the rest of your code, but a better way might be to use an object.
var data = {foo: "bar"};
var x = "foo";
data[x]; //=> bar
var y = "hello";
data[y] = "panda";
data["hello"]; //=> panda
I think this is a little cleaner and self-contained than the window approach

Categories