0

I have small problem. I have script that has many arrays. My previous function returns a "chosen" variable which is a parameter of my setBreakValues function it's also the name of array i want to access values of in my function. Lets say i get "test" return from my other function and pass it into setBreakValues i want to access test[0] element in test array but it returns the first letter of parameter. I just simply want to use it as a name of array. Is that possible?

Here is the broken code:

function setBreakValues(chosen){
    console.log(chosen[0][1])
    $("#totalTime").val($("#firstTime").val())
    var select = document.getElementById('breakValue');
    for(var i = 0; i < chosen.length; i++) {
        var option = document.createElement('option');
        option.innerHTML = chosen[i][0];
        option.value = chosen[i][0];
        select.appendChild(option);
    }
    $("#breakValue").change(function(){
        $("#secondPressure").val(chosen[0][1])
    })
}
dhilt
  • 18,707
  • 8
  • 70
  • 85
Jan Kowalski
  • 263
  • 2
  • 5
  • 13
  • if you get "test" returned then return[0] is "t" it referres to the first character in the string – john Smith Jan 11 '18 at 21:39
  • yes but how i can use that "test" to be the name of an array? is that possible? – Jan Kowalski Jan 11 '18 at 21:40
  • 1
    If the array is a global, you can use `window[arrayName]` to get the array. –  Jan 11 '18 at 21:40
  • 1
    or you have your arrays as properties of an object you could call them by `object[arrayName]` – john Smith Jan 11 '18 at 21:43
  • window[arrayName] works but not completly. I've got some dynamic content changed and after few changes it throws me errors with undefined arrays. Is there any other way around window[arrayName] ? – Jan Kowalski Jan 11 '18 at 21:47
  • Any time you find yourself wanting to do this, you should probably be using an object whose keys are the possible arguments. – Barmar Jan 11 '18 at 21:59

0 Answers0