@technobuddha/library
Version:
A large library of useful functions
17 lines (16 loc) • 632 B
JavaScript
import isFunction from 'lodash/isFunction';
/**
* Create a two dimensional array with all elements initialized
*
* @remark Array is accessed by array[w][h]
*
* @param width Width of the array
* @param height Height of the array
* @param fill value to fill the array, or a function returning the fill value for each element
*/
export function create2DArray(width, height, fill) {
if (isFunction(fill))
return Array.from(new Array(width), (_1, x) => Array.from(new Array(height), (_2, y) => fill(x, y)));
return Array.from(new Array(width), () => new Array(height).fill(fill));
}
export default create2DArray;