diff --git a/snippets/initialize2DArray.md b/snippets/initialize2DArray.md new file mode 100644 index 000000000..45047b819 --- /dev/null +++ b/snippets/initialize2DArray.md @@ -0,0 +1,12 @@ +### initialize2DArray + +Initializes an 2D array of given width and height and value. + +Use `.map()` to generate h rows where each is a new array of size w initialize with value. + +```js +const initialize2DArray = (w, h, val = null) => + Array(h).fill().map(() => Array(w).fill(val)); +// initializeArrayWithRange(2, 2, 0) -> [[0,0], +// [0,0]] +```