csv-table-parser
v1.1.1
Published
convert csv to object array
Downloads
5
Readme
how to install
npm install csv-table-parser
what's this?
csv file convert to object array like below.
const { parser } = require('csv-table-parser')
const input = `name, age, email, isMember
alice, 15, [email protected], true
bob, 25, [email protected], false
`
parser(input)
/*
*[
* { name: "alice", age: 15, email: "[email protected]", isMember: true },
* { name: "bob", age: 25, email: "[email protected]", isMember: false }
*]
*/
you can also convert to array in array.
const { parser } = require('csv-table-parser')
const input = `alice, 15, [email protected], true
bob, 25, [email protected], false
`
parser(input, { type: 'array' })
/*
*[
* [ "alice", 15, "[email protected]", true ],
* [ "bob", 25, "[email protected]", false ]
*]
*/
options
second parameter is options. the example is below.
separator
const input = `name age email isMember
alice 15 [email protected] true
bob 25 [email protected] false
`
parser(input, { separator: '\t' })
/*
*[
* { name: "alice", age: 15, email: "[email protected]", isMember: true },
* { name: "bob", age: 25, email: "[email protected]", isMember: false }
*]
*/
startRow
const input = `customer name, customer age, customer email for customer service, this user is member or not
name, age, email, isMember
alice, 15, [email protected], true
bob, 25, [email protected], false
`
parser(input, { startRow: 1 })
/*
*[
* { name: "alice", age: 15, email: "[email protected]", isMember: true },
* { name: "bob", age: 25, email: "[email protected]", isMember: false }
*]
*/
startColumn
const input = `no, name, age, email, isMember
1, alice, 15, [email protected], true
2, bob, 25, [email protected], false
`
parser(input, { startColumn: 1 })
/*
*[
* { name: "alice", age: 15, email: "[email protected]", isMember: true },
* { name: "bob", age: 25, email: "[email protected]", isMember: false }
*]
*/
numberOfColumn
const input = `name, age, email, isMember
alice, 15, [email protected], true
bob, 25, [email protected], false
`
parser(input, { numberOfColumn: 3 })
/*
*[
* { name: "alice", age: 15, email: "[email protected]" },
* { name: "bob", age: 25, email: "[email protected]" }
*]
*/
defaultValue
const input = `name, age, email, isMember
alice, 15, [email protected],
bob, 25, [email protected], true
`
parser(input, { defaultValue: false })
/*
*[
* { name: "alice", age: 15, email: "[email protected]", isMember: false },
* { name: "bob", age: 25, email: "[email protected]", isMember: true }
*]
*/