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

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]

Related

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

How to use an object's value as the parameter for another object in Javascript? [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 6 years ago.
If I have:
var key = "Section";
course.key = "101";
I get told course.key is unidentified. I'm trying to set course.Section = "101". Is there anyway to pass key's value in the second line?
The property key of course is undefined. Use bracket notation.
course[key] = "101"
is the same as
course["Section"] = "101"

How to get the value dynamically from JSON object [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 7 years ago.
I want to retrieve a JSON value dynamically from JSON object. Below is the code i am using to get the value from JSON object.
var jsonObj = JSON.parse(jsonData);
console.log(jsonObj);
jsonSplit = jsonToFind.split(htmlSplit+".")[1].trim();
console.log(jsonObj+"."+jsonSplit);
But I am getting [object Object].ensighten_tag.
Here ensighten_tag is the dynamic key value.
Can anyone suggest me how to get the value dynamically ?
console.log(JsonObj[jsonSplit])

Using the value of a string variable as object parameter accessor [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 8 years ago.
I am attempting to dynamically access an object with a property value that is contained within a string. Example below:
var toolState = {
draw_point: false;
draw_line: false;
}
var dynamicText = "draw_point";
toolState.dynamicText = true; //here is the problem
I'm fairly new to JS. Sorry if this is a silly question.
Thanks
Use bracket notation instead of dot notation for variable names as properties.
toolState[dynamicText] = true;

Updating a result in JSON with a dynamic variable name [duplicate]

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]

Categories