项目作者: aichbauer

项目描述 :
Convert a csv formatted string to an array
高级语言: JavaScript
项目地址: git://github.com/aichbauer/node-convert-csv-to-array.git
创建时间: 2018-04-05T10:18:04Z
项目社区:https://github.com/aichbauer/node-convert-csv-to-array

开源协议:MIT License

下载


convert-csv-to-array

npm
Travis branch
Codecov branch

Convert a csv formatted string to an array

Table of Contents

Why?

I needed a simple way to convert csv formatted string to an array to display it in a table component.

Installation

  1. $ npm i convert-csv-to-array -S

or

  1. $ yarn add convert-csv-to-array

Functions

Take a look into the usage section for a detailed example.

convertCSVToArray

Note: you can also use the default export.

This function converts a csv formatted string into an array of objects, or into an array of arrays.

Syntax

Returns a new array.

  1. const array = convertCSVToArray(data, options);
Parameters
  • data: a csv formatted string
  • options: a object
    • holds three keys: header, type and separator
    • header: either true or false, default: true
    • type: either 'object'or 'array', default: 'object'
    • separator: the character which is the separator in your csv formatted string, default: ','

Usage

Some examples on how to use this library.

  1. const { convertCSVToArray } = require('convert-csv-to-array');
  2. const converter = require('convert-csv-to-array');
  3. // would be a use input (upload) or read from a file
  4. const data = 'number;first;last;handle\n1;Mark;Otto;@mdo\n2;Jacob;Thornton;@fat\n3;Larry;the Bird;@twitter\n';
  5. /*
  6. const arrayofArrays = [
  7. ['number', 'first', 'last', 'handle'],
  8. [1, 'Mark', 'Otto', '@mdo'],
  9. [2, 'Jacob', 'Thornton', '@fat'],
  10. [3, 'Larry', 'the Bird', '@twitter'],
  11. ];
  12. */
  13. const arrayofArrays = convertCSVToArray(data, {
  14. type: 'array',
  15. separator: ';', // use the separator you use in your csv (e.g. '\t', ',', ';' ...)
  16. });
  17. /*
  18. const arrayofObjects = [
  19. ['number', 'first', 'last', 'handle'],
  20. {
  21. number: 1,
  22. first: 'Mark',
  23. last: 'Otto',
  24. handle: '@mdo',
  25. },
  26. {
  27. number: 2,
  28. first: 'Jacob',
  29. last: 'Thornton',
  30. handle: '@fat',
  31. },
  32. {
  33. number: 3,
  34. first: 'Larry',
  35. last: 'the Bird',
  36. handle: '@twitter',
  37. },
  38. ];
  39. */
  40. const arrayofObjects = convertCSVToArray(data, {
  41. separator: ';', // use the separator you use in your csv (e.g. '\t', ',', ';' ...)
  42. });
  43. /*
  44. const arrayofArraysWithoutHeader = [
  45. [1, 'Mark', 'Otto', '@mdo'],
  46. [2, 'Jacob', 'Thornton', '@fat'],
  47. [3, 'Larry', 'the Bird', '@twitter'],
  48. ];
  49. */
  50. const arrayofArraysWithoutHeader = convertCSVToArray(data, {
  51. header: false,
  52. type: 'array',
  53. separator: ';', // use the separator you use in your csv (e.g. '\t', ',', ';' ...)
  54. });
  55. /*
  56. const arrayofObjectsWithoutHeader = [
  57. {
  58. number: 1,
  59. first: 'Mark',
  60. last: 'Otto',
  61. handle: '@mdo',
  62. },
  63. {
  64. number: 2,
  65. first: 'Jacob',
  66. last: 'Thornton',
  67. handle: '@fat',
  68. },
  69. {
  70. number: 3,
  71. first: 'Larry',
  72. last: 'the Bird',
  73. handle: '@twitter',
  74. },
  75. ];
  76. */
  77. const arrayofObjectsWithoutHeader = convertCSVToArray(data, {
  78. header: false,
  79. separator: ';', // use the separator you use in your csv (e.g. '\t', ',', ';' ...)
  80. });

License

MIT © Lukas Aichbauer