Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
Javascript: load data from a local csv dataset in a 2d array in one function and then be able to loop that array in another function

My problem is related to javascript and csv file. The cvs file is from a excel and is located on the server side. My final Goal is to to be able to do some statistics with these data...

I tried different methods using papaparse, csv-parser and I was getting some errors.

the following way seems to have some issue:

I used a asynch function getData() to convert it into a 2d array and it is working:

        const rep=await fetch('datan2.csv')
        const data=await rep.text();
        let fin=[]
        const table=data.split('\n').slice(1)
        table.forEach(row=>{
        const colums=row.split(',');
        tab=[colums[0], colums[1],colums[2]]
        fin.push(tab)  
        });
        return fin
    }
    ```

From the following code, I can easily loop the array **fin** 

The problem now is when I try to get this array outside this function:

for example in the following code, where printing **t** is working but I cannot access a specific element of the array:

```t=getData()
    console.log(t)//working
    console.log(t[0][0])// not working here```
    

Can someone help? because my final goal is to be able to loop the array out side the **getData** method.

Thanks
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try replacing forEach with For..of. Like so

Replace

table.forEach(row=>{
   const colums=row.split(',');
   tab=[colums[0], colums[1],colums[2]]
   fin.push(tab)  
});

With

for(const row of table){
   const colums=row.split(',');
   tab=[colums[0], colums[1],colums[2]]
   fin.push(tab)
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda