Novice in node.js here. I am using csvtojson
to read a csv into an object fileDataObject
, like that:
const csv=require("csvtojson");
csv(
{
delimiter:mq.delim
,trim:false
}
)
.fromFile(mq.folder_pending+'\\'+file)
.then((fileDataObject)=>{
What I'd like is for fileDataObject
to have an additional "column" (property) named row_number
, which indicates the source line number of the file.
The documentation mentions something about a file line hook, but I don't know how to use hooks.
There is already an example on the package readme.
Once you get your fileDataObject
(in your example), you can simply add a column key
For example
const csv=require('csvtojson')
csv()
.subscribe((jsonObj,index)=>{
jsonObj.myNewKey='some value'
// OR asynchronously
return new Promise((resolve,reject)=>{
jsonObj.myNewKey='some value'; <-- add column here
resolve();
})
})
.on('data',(jsonObj)=>{
console.log(jsonObj.myNewKey) // some value
});
For your example
const csv=require("csvtojson");
csv(
{
delimiter:mq.delim
,trim:false
}
)
.fromFile(mq.folder_pending+'\\'+file)
.then((fileDataObject)=>{
//if you already have a javascript object, u can manually do it too.
let counter = 1;
const fileDatWithRowNumber = fileDataObject.map( item => {
item.row_number = counter;
counter++;
return item
})
return fileDatWithRowNumber
}