Update top JS snippets

This commit is contained in:
Chalarangelo
2021-10-13 20:09:28 +03:00
parent 2b775e3c93
commit 3cd481c0d1
19 changed files with 45 additions and 43 deletions

View File

@ -2,15 +2,15 @@
title: JSONtoCSV
tags: array,string,object,advanced
firstSeen: 2018-07-06T20:25:46+03:00
lastUpdated: 2020-10-22T20:23:47+03:00
lastUpdated: 2021-10-13T19:29:39+02:00
---
Converts an array of objects to a comma-separated values (CSV) string that contains only the `columns` specified.
- Use `Array.prototype.join(delimiter)` to combine all the names in `columns` to create the first row.
- Use `Array.prototype.map()` and `Array.prototype.reduce()` to create a row for each object, substituting non-existent values with empty strings and only mapping values in `columns`.
- Use `Array.prototype.map()` and `Array.prototype.reduce()` to create a row for each object. Substitute non-existent values with empty strings and only mapping values in `columns`.
- Use `Array.prototype.join('\n')` to combine all rows into a string.
- Omit the third argument, `delimiter`, to use a default delimiter of `,`.
- Omit the third argument, `delimiter`, to use a default delimiter of `','`.
```js
const JSONtoCSV = (arr, columns, delimiter = ',') =>