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.
Related
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]
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
how to get data from object with define string?
case:
var data = [
{name:"Sharma",country:"India"},
{name:"Udin",country:"Indonesia"},
{name:"John Carter",country:"Mars"}
];
getData(data,"country");
function getData(data,element){
console.log(data[1].element);
}
i want to get country but result is undefinded, how to fix this?
You would need to know both the index and the property
function getData(data,index,element){
console.log(data[index][element]);
}
getData(data,1,"country");
function getData(data,element){
console.log(data[1][element]);
}
That's the correct way to access the value by using a key that is a string.
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.
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 7 years ago.
I have a json object like
var json={
key1:val1,
key2:"foo" //as a string
}
and i have foo function
$scope.foo=function{
//something
}
Now on ng-click I want to do something like ng-click="json.key2()"
Basically I want to call this foo function from it's string name ?
Is it possible ?
I know it's bad approach, but is it possible ?
Thanks
You can access properties on an object dynamically by using square-brackets.
For example, in your controller (after declaration of $scope.foo):
$scope.fn = $scope[json.key2];
And in your markup:
ng-click="fn()"
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);