Tengo una función que lee varios archivos json . Ahora quiero modificarlo para que también pueda leer archivos csv . Tengo otra función que convierte texto en matrices json. Estoy tratando de hacer esto, pero recibo un error Property 'csvtoarray' does not exist on type 'FileReader'.ts(2339)
readFile(reader: FileReader, file: File) { const deferredResult = new Promise(resolve => { reader.addEventListener('load', function getResult() { console.log(file) if(file["type"] == "application/json"){ resolve(reader.result as string)} else if(file["type"]=="text/csv"){ resolve(this.csvtoarray(reader.result)) } reader.removeEventListener('load', getResult) }) }) ¿Cómo puedo llamar a mi función que convierte texto en matrices json en esta función readFile ?
Mi función que convierte texto en matrices json es:
csvtoarray(str,delimiter=','){ const headers = str.slice(0,str.indexOf("\n")).split(delimiter) const rows = str.slice(str.indexOf("\n") + 1).split("\n") const arr = rows.map(function(row) { const values = row.split(delimiter); const el = headers.reduce(function(object,header,index){ object[header] = values[index]; return object },{}); return el }); return arr }