Accessing variable value by string name [duplicate] - javascript

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
How can I access local scope dynamically in javascript?
(4 answers)
Closed 3 years ago.
Issue: I want to access some variables I got in my js-file by a string.
I know there is an option to receive variable value if you have an object and pass the object key name in square brackets to the object, but this is not the case here. Here I have just one comon variable, and I want its value by "sending" a string down.
Example on what I want to achieve:
const constantName = 12345; // Value to access
const stringToUseToGetTheValueOfConstantName = 'constantName';
// I want to know how to get the '12345' by something like this:
const valueFinallyAccessed = `${stringToUseToGetTheValueOfConstantName}`; // I know this returns a string
console.log(valueFinallyAccessed); // 12345
Solution
const valueFinallyAccessed = eval(stringToUseToGetTheValueOfConstantName);
console.log(valueFinallyAccessed); // Prints out the value 12345

Related

How to update an attribute in object based on a variable in javascript [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
JavaScript set object key by variable
(8 answers)
Closed 2 years ago.
I want to update an attribute of an object but the attribute is not known before hand and is rather stored in a variable.
I have an object "obj" like below
var obj={ a : 1, b : 2 }
if I want to change attribute "a" to 3 then I can just do
obj.a = 3
but I have a variable "attribute_to_change" which has the attribute that I want to change. I want to be able to somehow use the variable value to change the respective object attribute.
Something like below
obj.attribute_to_change = 3
Currently I have to write a separate line code of for each type of attribute that I want to change. But I can actually just make one function and pass the value and the attribute to be changed if I figure this out.

Setting JSON key with indexed array value [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 4 years ago.
I am trying to populate a JSON object by automatically setting the keys for an JSON object based on the string value of another array. For example,
var test = ["a","b"]
{test[0]:"A"}
However, I get a Syntax error when I do this, if I manually set the value as the string as shown in the third line {"a":"A"} this issue does not happen. I've checked that test[0] does indeed print out "a" and its datatype is a string. Is there any reason why this might be happening?
Try the following:
var test = ["a","b"]
var obj = {
[test[0]]:"A"
};
console.log(obj);

Is it possible to create dynamic keys in a 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 5 years ago.
for example I ask the user to enter an input and he enters "key" and I save the value on a variable called INPUT.
Then I want to create an object with this like this.
var obj = {INPUT: "some other input"}; where INPUT = key
I know I can add more values but I need to know the key ahead of time in order to add it. Can I add a new key without knowing what it is?
Yes, with computed property names. Assuming INPUT is an actual variable, simply wrap it in [].
var obj = {[INPUT]: "some other input"};
The long version of doing this would be to use bracket notation when adding your keys.
var obj = {};
obj[INPUT] = "some other input";

Double String Variable Name (Javascript) [duplicate]

This question already has answers here:
JavaScript Dynamic Variable Names [duplicate]
(6 answers)
Convert string to variable name in JavaScript
(11 answers)
Closed 7 years ago.
I know there are other questions similar to this, but I feel as if they don't answer my question. I would basically like to make Dynamic Variable Names. Let me give you an example:
//Take the strings "new" and "variable" and make them into a variable.
"new" + "variable" = "Hello World";
//So technically the variable name is "newvariable" and has the value "Hello World"
So basically it takes two strings, and combines them into one variable name. How would I go about doing this?
P.S. This is NOT to combine the values of variables, just the names
Write it into an array or object
var arr = {};
arr['new' + 'variable'] = 'Hello World';
then
alert(arr['newvariable']);
or
alert(arr.newvariable);

Get the name property of a variable [duplicate]

This question already has answers here:
Get variable name. javascript "reflection"
(4 answers)
JavaScript: Get Argument Value and NAME of Passed Variable [duplicate]
(7 answers)
Closed 8 years ago.
I was wondering if it's possible to get the name of a variables in javascript or JQuery.
Suppose that I declare a variable in javascript like:
var customerNr = "456910";
If a function receive a variable, how can i return the name of the variable?
For example:
function getNameOfVariable(someVariable){
//return name of someVariable;
}
If I call this function like this:
getNameOfVariable(customerNr);
The function has to return a string whose value is "customerNr".
How can I do this in jquery or javascript? Some kind of reflection?
That is simply not possible!
The passed parameter doesn't even have to have a name. It could be a return value of a function or a raw literal (like "string" or 3).
No, this is not possible. Only values are transferred for primitive data types, not references, so how would you know what the "name" of the variable is? For example:
var customerNr="456910";
var customerNrAfterProcessing=customerNr;
getNameOfVariable(customerNrAfterProcessing);
Would it return customerNrAfterProcessing or customerNr?
However, you can imitate this behavior with objects.
var variableName = someMethodThatDeterminesVariableNameOrJustAConstantIfYouPrefer();
var myVariables={};
var myVariables[variableName]={};
myVariables[variableName].value="456910";
myVariables[variableName].variableName=variableName;
function getNameOfVariable(someVariable){
return someVariable.variableName;
}
getNameOfVariable(myVariables[variableName]);
Obviously, this amounts to a lot more work than just using the variableName variable directly, but it could be necessary in some more complicated situations.
See working with objects in JS reference

Categories

Resources