function flatten(array) {
  if (!Array.isArray(array)) {
    // if not an array, return as is
    return array;
  }

  var flat = [];
  array.forEach(function callback(value) {
    if (Array.isArray(value)) {
      value.forEach(callback); // traverse through sub-arrays recursively
    } else {
      flat.push(value);
    }
  });
  return flat;
}
Flatten a multi dimensional array, put all elements in a one dimensional
array
@param {Array} array   A multi dimensional array
@return {Array}        The flattened array (1 dimensional)