Using a String Variable as a Variable Name

Convert string to variable name in JavaScript

If it's a global variable then window[variableName]
or in your case window["onlyVideo"] should do the trick.

How to use string value as a variable name in Python?

I don't understand what exactly you're trying to achieve by doing this but this can be done using eval. I don't recommend using eval though. It'd be better if you tell us what you're trying to achieve finally.

>>> candy = ['a','b','c']
>>> fruit = ['d','e','f']
>>> snack = ['g','h','i']
>>> name = 'fruit'
>>> eval(name)
['d', 'e', 'f']

EDIT

Look at the other answer by Sнаđошƒаӽ. It'll be better way to go. eval has security risk and I do not recommend its usage.

Variable name as a string in Javascript

Typically, you would use a hash table for a situation where you want to map a name to some value, and be able to retrieve both.

var obj = { myFirstName: 'John' };obj.foo = 'Another name';for(key in obj)    console.log(key + ': ' + obj[key]);

Using a variables string value in variable name

You're looking for Get-Variable -ValueOnly:

Write-Host $(Get-Variable "vari$part" -ValueOnly)

Instead of calling Get-Variable every single time you need to resolve a ListBox reference, you could pre-propulate a hashtable based on the partial names and use that instead:

# Do this once, just before launching the GUI:
$ListBoxTable = @{}
Get-Variable OBJ*_ListBox|%{
$ListBoxTable[($_.Name -replace '^OBJ(.*)_ListBox$','$1')] = $_.Value
}

# Replace your switch with this
foreach($Item in $OBJResults_ListBox.SelectedItems) {
$ListBoxTable[$SearchType].Items.Add($Item)
}

String Variables to Variable Name?

You can basically accomplish this with a dictionary. If you read the contents of the file into a dictionary with the key being the variable name and the score being the value. You've got your dynamic variables.

This is pretty much how python works behind the scenes. It uses a dictionary to store all variables and their values.

How to make string act like a variable name?

Specify the dictionary first

Then you can take id's object... /p>

const dictionary = {
id1: {name: 'xxxx'},
id2: {name: 'yyyy'},
id3: {name: 'zzzz'},
};

const myfunc = (item)=>{
const idName = item.id;
const aName = dictionary[idName];
console.log(aName.name);
};
<div id="id1" onclick="myfunc(this)">one</div>
<div id="id2" onclick="myfunc(this)">one but 2</div>
<div id="id3" onclick="myfunc(this)">the third</div>

Set string to variable name

You can store the data in an object with the keys matching your select's values.

var data = {
basketball: "changeable-string",
handball: 760,
football: null,
baseball: "description: ball-game to play"
}

function myFunction(el) {
console.log(data[el.value])
}
<select name="box" onchange="myFunction(this);">
<option value="football">Football</option>
<option value="handball">Handball</option>
<option value="basketball">Basketball</option>
<option value="baseball">Baseball</option>
</select>

Call string with a variable name

If I understand correctly, R.string.stringVariable obviously will not work since R.string is a compiled resource class and stringVariable needs to be a dynamic value.

You can use these options instead

int resID = getResources().getIdentifier(stringVariable, "string", getPackageName());

textview.setText(resID); // directly set it
String content = getString(resID); // get the variable, then concatenate with other content

To use a string value as a variable name

What you can do is by associating (mapping) those values to the Music object. Here is example:

Map<String, Music> musics = new HashMap<>();
String musicPlaying = "music2";
musics.put("music1", new Music("blaalla"));
musics.put("music2", new Music("blalala"));
musics.put("music3", new Music("balaada"));

if(!musics.get(musicPlaying).stillPlaying) { // As you can see i am using string as a variable name.
changeMusic();
}


Related Topics



Leave a reply



Submit