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

156
Visualizações
I have and error while converting CSV to Array

Is there any way of fixing this CSV error after I convert a CSV to an array? Console log the objects in the array, the last property has an error

Code

const csvFileToArray = (string, delimiter = ",") => {
    const csvHeader = string
        .slice(0, string.indexOf("\n"))
        .split(delimiter);
    const csvRows = string.slice(string.indexOf("\n") + 1).split("\n");

    const array = csvRows.map((row) => {
        const values = row.split(delimiter);
        const obj = csvHeader.reduce((object, header, index) => {
            object[header] = values[index];
            return object;
        }, {});
        console.log(obj)
        return obj;
    });
    localStorage.setItem("bulkUpload", JSON.stringify(array));
    handleClose();
    return setArray(array);
};

Output

{
    "category": "5f1a2779-f4b7-4e0d-a8fd-d2fff7e04418",
    "name": "product",
    "bulk_image": "image_url",
    "quantity": "18",
    "currency": "USD",
    "vendor": "815c5741-ed74-454f-85cd-c8f0a1aeee64",
    "price\r": "1399\r"
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Phuzi's solution worked. Terminating with a single \n was the issue. It should be terminated with \r\n.

Here is the correct code

const csvHeader = string
        .slice(0, string.indexOf("\r\n"))
        .split(delimiter);
    const csvRows = string.slice(string.indexOf("\r\n") + 1).split("\r\n");
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