diff --git a/README.md b/README.md index eedc27383..450d94c05 100644 --- a/README.md +++ b/README.md @@ -862,15 +862,16 @@ Use `Array.join('\n')` to combine all rows into a CSV string, separating each ro Omit the second argument, `delimiter`, to use a default delimiter of `,`. ```js -const arrayToCSV = (arr, delimiter = ',') => arr.map(v => v.join(delimiter)).join('\n'); +const arrayToCSV = (arr, delimiter = ',') => + arr.map(v => v.map(x => `"${x}"`).join(delimiter)).join('\n'); ```
Examples ```js -arrayToCSV([['a', 'b'], ['c', 'd']]); // 'a,b\nc,d' -arrayToCSV([['a', 'b'], ['c', 'd']], ';'); // 'a;b\nc;d' +arrayToCSV([['a', 'b'], ['c', 'd']]); // '"a","b"\n"c","d"' +arrayToCSV([['a', 'b'], ['c', 'd']], ';'); // '"a";"b"\n"c";"d"' ```
diff --git a/docs/array.html b/docs/array.html index 39df8ddc4..c69f5f8ce 100644 --- a/docs/array.html +++ b/docs/array.html @@ -85,9 +85,10 @@

any

Returns true if the provided predicate function returns true for at least one element in a collection, false otherwise.

Use Array.some() to test if any elements in the collection return true based on fn. Omit the second argument, fn, to use Boolean as a default.

const any = (arr, fn = Boolean) => arr.some(fn);
 
any([0, 1, 2, 0], x => x >= 2); // true
 any([0, 0, 1, 0]); // true
-

arrayToCSV

Converts a 2D array to a comma-separated values (CSV) string.

Use Array.map() and Array.join(delimiter) to combine individual 1D arrays (rows) into strings. Use Array.join('\n') to combine all rows into a CSV string, separating each row with a newline. Omit the second argument, delimiter, to use a default delimiter of ,.

const arrayToCSV = (arr, delimiter = ',') => arr.map(v => v.join(delimiter)).join('\n');
-
arrayToCSV([['a', 'b'], ['c', 'd']]); // 'a,b\nc,d'
-arrayToCSV([['a', 'b'], ['c', 'd']], ';'); // 'a;b\nc;d'
+

arrayToCSV

Converts a 2D array to a comma-separated values (CSV) string.

Use Array.map() and Array.join(delimiter) to combine individual 1D arrays (rows) into strings. Use Array.join('\n') to combine all rows into a CSV string, separating each row with a newline. Omit the second argument, delimiter, to use a default delimiter of ,.

const arrayToCSV = (arr, delimiter = ',') =>
+  arr.map(v => v.map(x => `"${x}"`).join(delimiter)).join('\n');
+
arrayToCSV([['a', 'b'], ['c', 'd']]); // '"a","b"\n"c","d"'
+arrayToCSV([['a', 'b'], ['c', 'd']], ';'); // '"a";"b"\n"c";"d"'
 

bifurcate

Splits values into two groups. If an element in filter is truthy, the corresponding element in the collection belongs to the first group; otherwise, it belongs to the second group.

Use Array.reduce() and Array.push() to add elements to groups, based on filter.

const bifurcate = (arr, filter) =>
   arr.reduce((acc, val, i) => (acc[filter[i] ? 0 : 1].push(val), acc), [[], []]);
 
bifurcate(['beep', 'boop', 'foo', 'bar'], [true, true, false, true]); // [ ['beep', 'boop', 'bar'], ['foo'] ]
diff --git a/snippets/arrayToCSV.md b/snippets/arrayToCSV.md
index e0ecc3bc1..4f206e932 100644
--- a/snippets/arrayToCSV.md
+++ b/snippets/arrayToCSV.md
@@ -7,7 +7,8 @@ Use `Array.join('\n')` to combine all rows into a CSV string, separating each ro
 Omit the second argument, `delimiter`, to use a default delimiter of `,`.
 
 ```js
-const arrayToCSV = (arr, delimiter = ',') => arr.map(v => v.map(x => `"${x}"`).join(delimiter)).join('\n');
+const arrayToCSV = (arr, delimiter = ',') =>
+  arr.map(v => v.map(x => `"${x}"`).join(delimiter)).join('\n');
 ```
 
 ```js