Javascript Can';t读取更新并保存到对象csv数据类型脚本/js
这是打字稿 有人能帮我做以下事情吗Javascript Can';t读取更新并保存到对象csv数据类型脚本/js,javascript,typescript,csv,parsing,Javascript,Typescript,Csv,Parsing,这是打字稿 有人能帮我做以下事情吗 我从CSV文件中读取数据 在飞行中转换此数据(删除一些额外的列) 然后,我想更新流中的csv返回到代码中的变量。 Console.log(updatedCsv)//在流中-显示我需要的内容 但是 当我尝试将其推入数组时,什么都不会发生,然后变量(我从流中推送数据的变量)被认为是未定义的: 从“fs”导入*作为fs; 从“csv”导入*作为csv udateCsv(){ fs.createReadStream('allure-report/data/suites
udateCsv(){
fs.createReadStream('allure-report/data/suites.csv'))
.pipe(csv.parse({分隔符:',,列:true}))
.pipe(csv.transform)(输入)=>{
console.log(input)//删除输入[header];
this.rowsArray=input//什么也不发生,rowsArray:string[]=new Array();input-我不知道是什么类型,也不知道是否使用push。我无法从管道中获取此数据
返回输入;
}))
.pipe(csv.stringify({header:true}))
.pipe(fs.createWriteStream(this.path))
以及
作为一种解决方法,我想阅读新生成的csv,但也没有成功,看起来我需要使用承诺。我尝试了一些来自internet的示例,但失败了。请帮助那些想知道的人-我能够使用以下方法解决我的目标: 但是!!我仍然想知道如何通过承诺、异步/等待方法来处理这个问题
class CsvFormatter{
pathToNotUpdatedCsv: string
readline: any
readStream: any
headers: any
fieldSchema: string[] = new Array()
rowsArray: string[] = new Array()
constructor(pathToCsv: string, encoding: string) {
this.pathToNotUpdatedCsv = pathToCsv
this.readStream = fs.createReadStream(this.pathToNotUpdatedCsv, encoding = 'utf8');
}
async updateCsv(){
//read all csv lines of not updated file
this.readline = readline.createInterface({
input: this.readStream,
crlfDelay: Infinity
});
//save them to array
for await (const line of this.readline) {
this.rowsArray.push(line)
}
//remove columns in csv and return updated csv array
this.rowsArray = this.getUpdatedRows()
//separating headers and other rows in csv
this.headers = this.rowsArray.shift()
}
getUpdatedRows(){
let headersBeforeUpdate = this.removeExtraQuotes(this.rowsArray[0])
let rowsAfterUpdate = []
let indexesOfColumnToDelete = []
let partOfUpdatedArray = []
//get indexes which will be used for deletion of headers and content rows
skipHeaders.forEach((header) => {
indexesOfColumnToDelete.push(headersBeforeUpdate.indexOf(header))
})
//delete rows by index
this.rowsArray.forEach(row => {
partOfUpdatedArray = this.removeExtraQuotes(row)
indexesOfColumnToDelete.forEach(index=>{
partOfUpdatedArray.splice(index)
})
rowsAfterUpdate.push(partOfUpdatedArray)
})
return rowsAfterUpdate
}
对于那些想知道的人-我能够使用以下方法解决我的目标: 但是!!我仍然想知道如何通过承诺、异步/等待方法来处理这个问题
class CsvFormatter{
pathToNotUpdatedCsv: string
readline: any
readStream: any
headers: any
fieldSchema: string[] = new Array()
rowsArray: string[] = new Array()
constructor(pathToCsv: string, encoding: string) {
this.pathToNotUpdatedCsv = pathToCsv
this.readStream = fs.createReadStream(this.pathToNotUpdatedCsv, encoding = 'utf8');
}
async updateCsv(){
//read all csv lines of not updated file
this.readline = readline.createInterface({
input: this.readStream,
crlfDelay: Infinity
});
//save them to array
for await (const line of this.readline) {
this.rowsArray.push(line)
}
//remove columns in csv and return updated csv array
this.rowsArray = this.getUpdatedRows()
//separating headers and other rows in csv
this.headers = this.rowsArray.shift()
}
getUpdatedRows(){
let headersBeforeUpdate = this.removeExtraQuotes(this.rowsArray[0])
let rowsAfterUpdate = []
let indexesOfColumnToDelete = []
let partOfUpdatedArray = []
//get indexes which will be used for deletion of headers and content rows
skipHeaders.forEach((header) => {
indexesOfColumnToDelete.push(headersBeforeUpdate.indexOf(header))
})
//delete rows by index
this.rowsArray.forEach(row => {
partOfUpdatedArray = this.removeExtraQuotes(row)
indexesOfColumnToDelete.forEach(index=>{
partOfUpdatedArray.splice(index)
})
rowsAfterUpdate.push(partOfUpdatedArray)
})
return rowsAfterUpdate
}