I got the dynamic variable name doing
varname = "data" + newid + "['" + name + "']";
I would like to assign a value to the dynamic variable. I tried this
eval(varname) = value;
but it doesn't work. What do I need to do in order to assign a value to the dynamic variable?
$$eval() method. This method runs Array. from(document. querySelectorAll(selector)) within the page and passes the result as the first argument to the pageFunction .
In strict mode, declaring a variable named eval or re-assigning eval is a SyntaxError . If the argument of eval() is not a string, eval() returns the argument unchanged. In the following example, the String constructor is specified and eval() returns a String object rather than evaluating the string.
A dynamic variable is a variable you can declare for a StreamBase module that can thereafter be referenced by name in expressions in operators and adapters in that module. In the declaration, you can link each dynamic variable to an input or output stream in the containing module.
The Eval function evaluates the string expression and returns its value. For example, Eval("1 + 1") returns 2. If you pass to the Eval function a string that contains the name of a function, the Eval function returns the return value of the function. For example, Eval("Chr$(65)") returns "A".
var data1 = { a: 200 };
var newid = 1;
var name = "a";
var varname = "data"+newid+"['"+name+"']";
var value = 3;
eval(varname + "=" + value); // change data1['a'] from 200 to 3
Having said that, eval
is evil. Are you really sure you need to use dynamic variables?
Don't use eval. Don't use dynamic variables.
If you have an unordered group of related data, store it in an object.
var myData = {};
myData[ newid + name ] = value;
although it looks like you are dealing with a dynamic object so
myData[ newid ] = myData[ newid ] || {};
myData[ newid ][ name ] = value;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With