diff --git a/README.md b/README.md index b19b22831..0036100e1 100644 --- a/README.md +++ b/README.md @@ -950,9 +950,7 @@ Use `Array.map()` to generate h rows where each is a new array of size w initial ```js const initialize2DArray = (w, h, val = null) => - Array(h) - .fill() - .map(() => Array(w).fill(val)); + Array.from({ length: h }).map(() => Array.from({ length: w }).fill(val)); ```
@@ -5202,6 +5200,7 @@ const httpPost = (url, callback, data = null, err = console.error) => { ```js + const newPost = { "userId": 1, "id": 1337, diff --git a/docs/index.html b/docs/index.html index 56386c4d0..53d6278d4 100644 --- a/docs/index.html +++ b/docs/index.html @@ -151,9 +151,7 @@ Object.assig

initial

Returns all the elements of an array except the last one.

Use arr.slice(0,-1) to return all but the last element of the array.

const initial = arr => arr.slice(0, -1);
 
initial([1, 2, 3]); // [1,2]
 

initialize2DArray

Initializes a 2D array of given width and height and value.

Use Array.map() to generate h rows where each is a new array of size w initialize with value. If the value is not provided, default to null.

const initialize2DArray = (w, h, val = null) =>
-  Array(h)
-    .fill()
-    .map(() => Array(w).fill(val));
+  Array.from({ length: h }).map(() => Array.from({ length: w }).fill(val));
 
initialize2DArray(2, 2, 0); // [[0,0], [0,0]]
 

initializeArrayWithRange

Initializes an array containing the numbers in the specified range where start and end are inclusive with there common difference step.

Use Array.from(Math.ceil((end+1-start)/step)) to create an array of the desired length(the amounts of elements is equal to (end-start)/step or (end+1-start)/step for inclusive end), Array.map() to fill with the desired values in a range. You can omit start to use a default value of 0. You can omit step to use a default value of 1.

const initializeArrayWithRange = (end, start = 0, step = 1) =>
   Array.from({ length: Math.ceil((end + 1 - start) / step) }).map((v, i) => i * step + start);
@@ -1164,6 +1162,7 @@ Logs: {
 };
 
 
+
 const newPost = {
   "userId": 1,
   "id": 1337,
diff --git a/snippets/httpPost.md b/snippets/httpPost.md
index b0938a722..9387c3afb 100644
--- a/snippets/httpPost.md
+++ b/snippets/httpPost.md
@@ -23,6 +23,7 @@ const httpPost = (url, callback, data = null, err = console.error) => {
 ```js
 
 
+
 const newPost = {
   "userId": 1,
   "id": 1337,
diff --git a/snippets/initialize2DArray.md b/snippets/initialize2DArray.md
index d7df444ce..dd2757533 100644
--- a/snippets/initialize2DArray.md
+++ b/snippets/initialize2DArray.md
@@ -6,7 +6,7 @@ Use `Array.map()` to generate h rows where each is a new array of size w initial
 
 ```js
 const initialize2DArray = (w, h, val = null) =>
-  Array.from({ length: h }).map(() => Array.from({ length: w}).fill(val));
+  Array.from({ length: h }).map(() => Array.from({ length: w }).fill(val));
 ```
 
 ```js