This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 1 year ago.
So I'm trying to get data from a JSON and I declared a variable and want to use the thing from that variable to get info from the JSON I'm trying to do this but I don't know what the problem is
let e = "KA8AL9AE";
let codes = '{"T9R3P5YE": "10", "8HAW69VC": "20"}'
codes = JSON.parse(codes)
console.log(codes.e)
I've tried doing
let e = "KA8AL9AE";
let codes = '{"T9R3P5YE": "10", "8HAW69VC": "20"}'
codes = JSON.parse(codes)
console.log(`${codes. + e}`)
But it didn't work, Is there a way to do this?
You could call the new key like:
codes[e]
Related
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:
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}})
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 5 years ago.
I want to get the value from array. It shows Type error. The code is
var user = "username6";
var result = categories.filter(x => x.user)[0].user;
TypeError: Cannot read property 'user' of undefined
But it work in
var result = categories.filter(x => x.username6)[0].username6;
I want to give the "username6" variable in user. And execute
var result = categories.filter(x => x.user)[0].user;
How it possible? Please help me?
You should use the brackets notation instead of the dot notation
var user = "username6";
var result = categories.filter(x => x[user])[0][user];
This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 8 years ago.
I need to update a value in my JSON.
My JSON result looks like this:
results = {"ROWCOUNT":50,"COLUMNS":["PERSONID","NAME"],"DATA":{"PERSONID":["42","43","44"], "NAME":["JOE","TOM","JANE"]}
resultData = results.DATA
In the below code I am looping over the result set and attempting to update a value at a position. I believe it is failing because I am not using dynamic variables correctly.
var columnName = "NAME";
for(i=0; i < results.ROWCOUNT; i++ ){
resultData.columnName[i] = "foo" // failing here due to "columnName" being dynamic.
}
Figured it out.. You have to use array syntax
resultData[columName][i]