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);
Related
This question already has answers here:
How to concatenate properties from multiple JavaScript objects
(14 answers)
Closed 6 months ago.
var obj={"student":[{"id":1,"name":"mark"}]}
For example I have this object I want to add another element to it such as this:
var teacher= "teacher":[{"id":1,"name":"Stacy"}]
to make this new object:
var objnew= {"student":[{"id":1,"name":"mark"}], "teacher":[{"id":1,"name":"Stacy"}] }
You can use this code.
var objnew = Object.assign({}, obj, teacher);
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]
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.
I have this snippet of code:
var arrLiteData = [];
for(ii=0;ii<10;ii++)
{
arrLiteData.push({ii:{"field1":100,"field2":ii}});
}
...but instead of ii taking the increasing numeric value of ii, the array just holds the actual variable name, like this:
[{"ii":{"field1":100,"field2":0}},{"ii":{"field1":100,"field2":1}}...etc, etc...
What am I doing wrong?
Many thanks.
Quotes are optional for javascript object keys, so
{ii:{"field1":100,"field2":ii}} is the same as
{"ii":{"field1":100,"field2":ii}} or even
{ii:{field1:100,field2:ii}}. They are just need if you have non alphanumeric characters.
To solve this you could either use a computed key if you're transpiling your code or targeting recent navigators:
{[ii]:{"field1":100,"field2":ii}}
Or build the object in two steps:
var arrLiteData = [];
for(ii=0;ii<10;ii++)
{
var obj = {};
obj[ii] = {"field1":100,"field2":ii};
arrLiteData.push(obj);
}
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
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}})