title: Parse array of arrays in JavaScript
body: <p>I need a function that would return a value from an array that may contain any number of arrays. It should be called like getValueFromArray(array, [2, 4]) - this example should return 4th element of the 2d array of the passed array.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>function getValueFromArray(arr, indexes){
  
  var val,
      currentIndex = indexes[0];
  
      if(!arr[currentIndex] || arr[currentIndex] === '') return value = '';
  
      indexes.splice(0, 1);
  
      if(arr[currentIndex].length) 
          getValueFromArray(arr[currentIndex], indexes);
      else {
          val = arr[currentIndex];
          return val;
      }
   }
  
  
        var y = getValueFromArray([[1,2,3,4], [1,2,3,4]], [0, 2]); // should return 3
  
        var x = getValueFromArray([[1,2,3,4], [1,2,3,4], [5,6,7,8]], [2, 3]); // should return 8
  
        var z = getValueFromArray(
                             [
                                 [[1,2,3,4], [1,2,3,4], [1,2,3,4]],
                                 [[1,2,3,4], [1,2,3,4]]
                             ], 
                             [0, 1, 2]
                            ); // should return 3
  </code></pre>
  
  <p>Such call should return 3, and if I debug the function, it actually returns the correct value but when I assign it to a variable, it returns undefined. I guess it's because of the recursion, variable gets the value that is undefined during the first function call. How can this be fixed?</p>
  
  <p>Thank you! </p>