简体   繁体   中英

Get Variables Data from two concatenated Strings

I want to get the data from the Variable "EDDBGnd". the debug Paragraph should display "This is a test".

var Apt = "EDDB";
var Menu = "Gnd";
var EDDBGnd = "This is a test";

document.getElementById("debug").innerHTML = Apt.concat(Menu);

If you have the ability to use a different method to store the data, I would recommend using an object instead. This will allow you to have the apt as a key that can hold multiple menu's for example.

It is more complex, but it will allow you to easily grow the object for more data without messing with variables.

 var data = { "EDDB": { "Gnd": "Test is a test", "Gnd2": "Test is also a test", } } document.getElementById("debug").innerHTML = data["EDDB"]["Gnd"]; document.getElementById("debug2").innerHTML = data["EDDB"]["Gnd2"];
 <div id="debug"></div> <div id="debug2"></div>

If EDDBGnd is a global variable you can write

document.getElementById("debug").innerHTML = window[Apt+Menu];

Try something like that:

document.getElementById("debug").elements[0].value = EDDBGnd;

You need to scope the global variables to the window object.

 var Apt = "EDDB"; var Menu = "Gnd"; var EDDBGnd = "This is a test"; document.getElementById("debug").innerHTML = window[window.Apt.concat(window.Menu)];
 <div id="debug"></div>

A better way is to create a context or localization file (or object).

 const context = { 'EDDB': { 'Gnd': 'This is a test' } }; const Apt = 'EDDB'; // From GUI? const Menu = 'Gnd'; // From GUI? const getText = (Apt, Menu) => context[Apt][Menu]; document.getElementById("debug").innerHTML = getText('EDDB', 'Gnd');
 <div id="debug"></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM