Reputation: 1581
I am trying to return a row value on a matching string.
I begin with a csv that doesn't have header details. After loading the csv into memory, I think I need to first add headers and then convert the data to json, then loop through the data to find the correct object in the array.
I have used highland to create a read stream and output to objects. However, the line break characters \r
and \r\n
are not getting parsed out and are injected into the value strings.
This approach also doesn't seem to parse the entire file, it is outputting 3 lines.
highland(fs.createReadStream('example.csv', 'utf8'))
.map(line => line.split(','))
.map(parts => ({
a: parts[0],
b: parts[1],
c: parts[2]
}))
.each(x => console.log(x))
It would be nice to structure the CSV as JSON and then use the .filter()
method to match the record.
Actual output of 1000+ line file
{ a: '', b: 'CD53110' }
{ a: '\nRD40115', b: 'CD40315' }
{ a: '', b: '63\r\nRE15468' }
{ a: '96798', b: '5\r\nRR60899' }
Example input snippet
,CD510,13
,T9069,65
RCM22,TC633,101
RC023,87693,16
M2024,T7636,109
Note: first few rows only contain 'b' and 'c' columns.
Upvotes: 0
Views: 2487
Reputation: 1581
You can write the headers out as a string, set the csv to memory, and concatenate the two.
Then with the csvtojson
module you can map the csv
to json
and use the native filter function to return the matching row.
const fs = require('fs')
const csv = require('csvtojson')
let headers = 'a,b,c\n'
let file = fs.readFileSync(__dirname + 'example.csv', 'utf8')
let strToMatch = 'def123'
let c = headers + file
csv().fromString(c)
.then((json) => {
let arr = json.filter(el => el.a == strToMatch)
res.json({
a: arr[0]['a'],
b: arr[0]['b'],
c: arr[0]['c']
})
})
Upvotes: 0