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.
Related
This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 7 months ago.
I have a flexible amount of titles, aka title1, title2, title3 etc. How can i create a dynamic variable that gives me the value of that field?
function getTitle(i){
var myScope={}
myScope["title"+i]="title"+i // should i use this in any way, and how?
var output = props.attributes.title1 // This works, but it is static. Instead the 1, add the value of i here like:
var output = props.attributes.title+i
return output
}
I would like to concatenate the value of i to the word 'title'. So it becomes title1 or title2 etc.
You can make it dynamic like this
var output = props.attributes[`title${i}`];
If I understood your question right you can use template literals.
function getTitle(i) {
let title = 'myTitle'
let output = `${title} ${i}`
console.log(output)
return output
}
getTitle('concated')
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.
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 5 years ago.
I wish to use a variable as json key in javascript
var chtid = "1234"
firebase.database().ref().set ({chtid :"hi"});
where chtid would be a variable
I tried this way but to no success
var chtid = "1234"
firebase.database().ref().set ({[chtid] :"hi"});
Any simple soulution please.
Put it on a variable first:
var obj = {};
var chtid = "1234"
obj[chtid] = "Hi";
firebase.database().ref().set (obj);
hope that helps
This question already has answers here:
Why would a JavaScript variable start with a dollar sign? [duplicate]
(16 answers)
Closed 5 years ago.
When you pass variables in Javascript functions, what is the difference between '$name' and 'name'.
For example,
var myFunction = function(name){
name.a = "a";
};
var myFunction = function($name){
$name.b = "b";
};
The only difference in your example is the name, it has no special purpose.
However you might have seen the $name styled variables in jQuery examples in which it's normal to name selected elements ($('a')) as, for example, $links.
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);