Examples
delimiter
Alternate You can change the default delimiter ,
by specifying the delimiter
option
- TypeScript
- JavaScript
- Output
rowDelimiter
Alternate You can change the default row delimiter \n
by specifying the rowDelimiter
option.
- TypeScript
- JavaScript
- Output
quote
Alternate You change change the default quote "
option by specifying the quote
option.
- TypeScript
- JavaScript
- Output
escape
Alternate You change change the default escape "
option by specifying the escpae
option.
- TypeScript
- JavaScript
- Output
Headers
Auto Discovery
fast-csv
will auto-discover headers when the headers
option is set to true
.
info
When working with one-dimensional array rows (e.g. ['a', 'b', 'c']
) this is a no-op.
In this example the headers are auto-discovered from the objects passed in.
- TypeScript
- JavaScript
- Output
In this example the headers are auto-discovered from the hash arrays passed in.
- TypeScript
- JavaScript
- Output
Provided Headers
You can also provide a set of headers
by providing an array. This allows you to
- Reorder and/or exclude columns when working with object rows.
- Rename and/or exclude columns when working with hash array rows.
- Specify headers or remove columns when working with array rows.
- Enforce column order, when rows are objects.
In this example a custom set of headers is provided for rows that are arrays.
- TypeScript
- JavaScript
- Output
In this example the headers are overridden with a custom set of headers
- TypeScript
- JavaScript
- Output
In this example the columns are reordered.
- TypeScript
- JavaScript
- Output
In this example one of the columns is removed.
- TypeScript
- JavaScript
- Output
Write Headers
The writeHeaders
option can be used to prevent writing headers, while still auto discovering them or providing them.
The writeHeaders
option can be useful when appending to a csv to prevent writing headers twice. See the append example
note
If writeHeaders is set to false, headers is set to true, and your rows are arrays, the first row will not be written.
Do not write headers.
In this example the auto discovered headers are not written.
- TypeScript
- JavaScript
- Output
Specify column order without headers
In this example the headers are provided to specify order of columns but they are not written.
- TypeScript
- JavaScript
- Output
quoteColumns
Sometimes you may need to quote columns in a certain ways in order to meet certain requirements. fast-csv
can quote columns and headers almost anyway you may need.
boolean
Setting quoteColumns
to true will by default quote all columns and headers.
- TypeScript
- JavaScript
- Output
boolean[]
Setting quoteColumns
to a boolean[]
will quote the columns that are set to true at each index in the array.
- TypeScript
- JavaScript
- Output
object
Setting quoteColumns
to a {[string]: boolean}
will quote the columns that are in the object with a value of true
- TypeScript
- JavaScript
- Output
Quote Columns NOT headers
If you need to quote columns and not headers you can set quoteHeaders
to false
.
- TypeScript
- JavaScript
- Output
quoteHeaders option
The quoteHeaders
option uses the same types as quoteColumns
.
- TypeScript
- JavaScript
- Output
In this example we specify the headers to quote by passing in an array to specify which headers to quote
- TypeScript
- JavaScript
- Output
In this example we specify the headers to quote by passing in an object where the key is the header name and the value is a boolean.
- TypeScript
- JavaScript
- Output
Transforming Rows
You can transform rows by using the .transform
method.
- TypeScript
- JavaScript
- Output
You can also specify your transform method as an option.
- TypeScript
- JavaScript
- Output
Transform can also be async by accepting a callback.
- TypeScript
- JavaScript
- Output
Appending To A CSV
In this example a new csv is created then appended to.
- TypeScript
- JavaScript
- Output