How can I turn a string into a key? [duplicate] - javascript

This question already has answers here:
Javascript create variable from its name
(5 answers)
Closed 8 years ago.
I need to run this line of Javascript:
#model.save({ name: #some_element.val() })
But the key, which in this case is name, will change depending on the value of a variable. The variable is a string representation of the key. So in this case, the variable is "name". How can I use the variable to specify the correct key? If I use the variable name directly it is interpreted as the key itself.

var obj = {};
obj[varName] = #some_element.val();
#model.save(obj);

Related

object name is same as variable [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 3 years ago.
Let's say I have an object
const myArray = {
a : "hello"
}
and I have a string with the same name of that object
like
var type ="myArray";
when I do console.log(type);
output: myArray
but I want to out that object to the console which has the same name as the value of variable type.
How should I do that?
Thanks in advance
If it's a global variable, it will be stored in window.
So, you can do something like console.log(window[type]) to access to value.

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]

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.

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"

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;

Categories