multidim-array-index.js
function multiDimArrayIndex (dimensions, indices) {
  // Check that indices fit inside dimensions shape.
  for (var i = 0; i < dimensions.length; i++) {
    if (indices[i] > dimensions[i]) {
      throw new TypeError(error.outOfBoundIndex)
    }
  }

  var order = dimensions.length

  // Handle order 1
  if (order === 1) return indices[0]

  //* index = i_n + i_(n-1) * d_n + i_(n-2) * d_n * d_(n-1) + ... + i_2 * d_n * d_(n-1) * ... * d_3 + i_1 * d_n * ... * d_2
  var n = order - 1
  var factor = dimensions[n] // d_n
  var index = indices[n] + factor * indices[n - 1] // i_n + i_(n-1) * d_n

  for (var j = 2; j < order; j++) {
    factor *= dimensions[n - j]

    index += factor * indices[n - j]
  }

  return index
}
function multiDimArrayIndex (dimensions, indices) {
  // Check that indices fit inside dimensions shape.
  for (var i = 0; i < dimensions.length; i++) {
    if (indices[i] > dimensions[i]) {
      throw new TypeError(error.outOfBoundIndex)
    }
  }

  var order = dimensions.length

  // Handle order 1
  if (order === 1) return indices[0]

  //* index = i_n + i_(n-1) * d_n + i_(n-2) * d_n * d_(n-1) + ... + i_2 * d_n * d_(n-1) * ... * d_3 + i_1 * d_n * ... * d_2
  var n = order - 1
  var factor = dimensions[n] // d_n
  var index = indices[n] + factor * indices[n - 1] // i_n + i_(n-1) * d_n

  for (var j = 2; j < order; j++) {
    factor *= dimensions[n - j]

    index += factor * indices[n - j]
  }

  return index
}
function multiDimArrayIndex ( dimensions , indices ) { // Check that indices fit inside dimensions shape. for ( var i = 0 ; i < dimensions . length ; i ++ ) { if ( indices [ i ] > dimensions [ i ] ) { throw new TypeError ( error . outOfBoundIndex ) } } var order = dimensions . length // Handle order 1 if ( order === 1 ) return indices [ 0 ] //* index = i_n + i_(n-1) * d_n + i_(n-2) * d_n * d_(n-1) + ... + i_2 * d_n * d_(n-1) * ... * d_3 + i_1 * d_n * ... * d_2 var n = order - 1 var factor = dimensions [ n ] // d_n var index = indices [ n ] + factor * indices [ n - 1 ] // i_n + i_(n-1) * d_n for ( var j = 2 ; j < order ; j ++ ) { factor *= dimensions [ n - j ] index += factor * indices [ n - j ] } return index }
Maps multidimensional array indices to monodimensional array index

Given

dimensions d_1, d_2, d_3 .. d_n
and
indices i_1, i_2, i_3 .. i_n

index is computed by formula
index = i_n + i_(n-1) * d_n + i_(n-2) * d_n * d_(n-1) + ... + i_2 * d_n * d_(n-1) * ... * d_3 + i_1 * d_n * ... * d_2

@param {Array} dimensions
@param {Array} indices
@returns {Number} index
Maps multidimensional array indices to monodimensional array index