Angular: Change String to variable [duplicate] - javascript

This question already has answers here:
Convert string to variable name in JavaScript
(11 answers)
Closed 6 years ago.
Apologies if this is simple. I just couldn't figure-out how to do it. Searched on this site on how to dynamically change variables and evaluate them etc but can't figure out how to do what I want.
Problem: I have a variable that I set to a value:
vm.toggleDrop = function($switchToggle){
vm.switchValue = "switch"+$switchToggle;
//Here
};
Where it says "Here", I need to instantiate a new variable that is called whatever is the result if the above statement eg; switch1 then set it true or false as a boolean. eg: switch1 = false;
Therefore again, if $switchToggle parameter was "Test", I need to dynamically create a variable called switchTest and set it true or false.
Is such a thing possible ?
Thanks all

Something like that?(I created vm variable just for code snippet, ignore it)
var vm = {};
vm.toggleDrop = function($switchToggle){
vm.switchValue = "switch"+$switchToggle;
vm[vm.switchValue] = 'whatever';
console.log(vm);
};
vm.toggleDrop('true');
Also if you do not need to attach variable to vm object, best answer will be #nastyklad provided(using window[vm.switchValue]

Something like this:
function setVariable(name){
var variableName = 'switch' + name;
vm[variableName] = true;
}

Related

Where can I place prototype property as per my below code? [duplicate]

This question already has answers here:
Assigning prototype methods *inside* the constructor function - why not?
(6 answers)
Closed 5 years ago.
I have writtern this two different ways and both giving me same result , so which I can use when ?
FIRST EXAMPLE
var BaseCls = function() {
BaseCls.prototype.name = "John";
};
var JustCls = new BaseCls();
console.log(JustCls.name); // This is giving result John
SECOND EXAMPLE
var BaseCls = function() {};
BaseCls.prototype.name = "John";
var JustCls = new BaseCls();
console.log(JustCls.name); // This is also giving result John
Both giving me same result so I just want to know is there any other criteria which lead to write this property / method with prototype inside / outside main function ?
Thanks for consideration
You should change prototype only outside the constructor.
Otherwise you change it every time you create an instance.

Using String to get JSON Property [duplicate]

This question already has an answer here:
Accessing a JSON property (String) using a variable
(1 answer)
Closed 8 years ago.
I want to use a string as a JSON property in JavaScript.
var knights = {
'phrases': 'Ni!'
};
var x = 'phrases';
console.log(knights.x); // Doesn't log "Ni!"
When I run this code, it obviously doesn't work because it interprets "x" and not the contents of the variable "x".
The full code in context on pastebin: http://pastebin.com/bMQJ9EDf
Is there an easy solution to this?
knights.x looks for a property named x. You want knights[x], which is equivalent to knights['phrases'] == knights.phrases.
Full code (fixing a couple of typos in your example):
var knights = {
"phrases": "Ni!"
};
var x = 'phrases';
console.log(knights[x]); // logs Ni!
Try this to access using variables having string values
kinghts[x]
Basically this is trick
kinghts[x]==knighted["phrases"]==knighted.phrases.
knights.x will get a key named x, So it'll return undefined here.
knights.x is the same as knights['x'] - retrieving a property under the key x. It's not accessing the variable x and substituting in the value. Instead, you want knights[x] which is the equivalent of knights['phrases']

Convert String name into javascript variable name? [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 8 years ago.
Problem with example:
Variable name: var product5519 = 10;
I can get this name in the form of a String i.e
var str = "product5519"
Is there any way to convert it into variable name so that i can use the value assigned to
product5519
I know one way to solve this problem i.e using eval(str)
If there is any another way to solve it please tell?
Once you are certain creating a global variable was the Right Thing to do, you can add your variable to the window object, like so:
window[str] = 42;
This works because variable lookups end up trying the window object if the variable was not defined in an inner scope.
It's a bit hacky but if you wanted to make a global variable you could do:
var str = "product5519";
window[str] = value;
You could then access the variable like:
window[str];
window.str;
str; // Assuming that there is no local variable already named "str"
you could do something like:
window['product5519'] = 'value'
it may be better to have an array of products, depending on the situation ofc.
You can use an associative array:
var variables = [];
variables['product5519'] = 10;
console.log(variables['product5519']);

Using string as variable name in JS? [duplicate]

This question already has answers here:
Is there a way to access a javascript variable using a string that contains the name of the variable?
(4 answers)
How to use a string as a variable name in Javascript? [duplicate]
(3 answers)
parse a string as an object from data attribute [duplicate]
(1 answer)
Use dynamic variable names in JavaScript
(19 answers)
Closed 9 years ago.
How can I make the code that follows to work?
var x = 'name';
and then, to use the value inside x, as it was a variable, and set it, so that if i want it to be NAME, i'll have the result:
var name = 'NAME';
Is it possible ?
Not directly, no. But, you can assign to window, which assigns it as a globally accessible variable :
var name = 'abc';
window[name] = 'something';
alert(abc);
However, a much better solution is to use your own object to handle this:
var name = 'abc';
var my_object = {};
my_object[name] = 'something';
alert(my_object[name]);
I haven't seen the rest of your code, but a better way might be to use an object.
var data = {foo: "bar"};
var x = "foo";
data[x]; //=> bar
var y = "hello";
data[y] = "panda";
data["hello"]; //=> panda
I think this is a little cleaner and self-contained than the window approach

Is it possible to make User defined DOM changes in runtime? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
javascript object, access variable property name?
I am messing around with JS, learning a stuff and I am wondering about something...
Say you have a function aFunc(), and you accept a string aFunc(val). The value is user defined and is then used to modify the CSS of an element.
For Example:
function aFunc(val){
document.getElementById('something').style.val = 'red';
}
Say the user entered borderColor, it would somehow refrence borderColor where val is. I do not know how or if this is possible.
EDIT:
Please no eval() :)
Just use this as a base: JSBIN- Demo on a Div
var type = prompt("style");
var value = prompt("value");
document.body.style[type] = value;
Every object in JavaScript has a method called hasOwnProperty which takes a string value and will return a Boolean value.
var myObj = {
name: "Josh"
};
myObj.hasOwnProperty("name") === true; //This is true
You can use that to test for the presence of a particular property and then use the method stated in Akhil Sekharan's answer to access that property.

Categories