0

I am attemtping to add strings together to create the name of a variable, which I can then check its value. The code may explain this better, to understand what I mean. I have several variables like below

var scList = ["stream", "dev", "web"];

var stream_sc = ["test0", "test1"];
var dev_sc = ["value","value"];
var web_sc = ["value", "value"];

In another function I am checking if entered text matches one of the values in scList, I then hope to add the string "_sc" to the end of the entered text, and from there be able to display the values inside the variables ending in _sc. The below is what I have so far. (With dataAr[1] being the user entered string I am checking for)

for (var i = 0; i < scList.length; i++) {
   if (dataAr[1] == scList[i]) {
      window.alert(scList[i]+"_sc"[0]);
   }
}

While it is able to match the string to a value in the scList Array properly, the value returned by the window alert is just the user entered string, not showing the "_sc", which makes sense but how could I get this to then relate to the matching variable ending in "_sc"?

To Clarify My hope is that if a user entered "stream" or if dataAr[1] == "stream" that the returned value would be "test0" from stream_sc[0] Thank you for any help!

2 Answers 2

1

You have an extraneous [0] after "_sc" which would grab the underscore and append it to the variable name. What you want is probably scList[i] + "_sc"

You should store them in a json object, so you can reference the sc names to retrieve the values, like so:

const varMap = {
  "stream_sc": [ ],
  "dev_sc": [ ],
  "web_sc": [ ]
};
// access the map using the entered value
varMap[scList[i] + "_sc"][j]
// where j would be the desired item index in the _sc array

I'm currently on a phone, so I hope the formatting comes out ok.

EDIT: fixed some syntax that was needed that the asker pointed out.

Sign up to request clarification or add additional context in comments.

1 Comment

This is the solution I ended up using. Thank you, although one edit to the syntax used was needed. Having the final code look something like this. varMap[dataAr[1]+"_sc"][0][0] But that worked perfectly for my project!
1

I assume you dataAR is an array as you make a reference to a key in the conditional dataAr[1]. You could use .includes() in that case to see if your array includes the value of scList[i]... I used a select to show how this could be achieved in a for loop and conditional. If the conditional is true, then rather that using the array scList[i] value use the value that is true... Then concatenate the _sc onto the value and define that as a new variable...

var sel = document.getElementById('sel');
var dataAr = ['gaming']; // array with a value that is not true present already

var scList = ["stream", "dev", "web"];

var stream_sc = ["test0", "test1"];
var dev_sc = ["value", "value"];
var web_sc = ["value", "value"];

function getVal(scList, sel) {
  for (var i = 0; i < scList.length; i++) {
    if (scList.includes(sel.value)) {
      val = sel.value + '_sc';
      return val;
    }
  }
}
let val;
sel.addEventListener("change", function(e) {
  dataAr.push(sel.value);  
  alert(getVal(scList, sel));
  // val myVar = getVal(scList, sel); <-- Do something with this var now
});
<select name="sel" id="sel">

  <option>--select an option below--</option>
  <option value="stream">Twitch</option>
  <option value="dev">Visual Studio Code</option>
  <option value="web">Mozilla</option>
</select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.