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

134
Visualizações
split csv where fields contain commas

Thanks to the answer posted here: Put data from a csv file into an array (Javascript) I have been able to split a csv file in the manner I need to but I have come across a problem with the output. Nothing to do with the code as it works just as I wanted it to. The issue relates to the source data.

The csv files are provided to me as is so I did not realise that they had fields that contained commas. This means the split doesn't work as needed.

This is the code I am using:

$.get("export.csv", function process(dataString) {
    var lines = dataString
    .split(/\n/)
    .map(function(lineStr) {
        return lineStr.split(",");
    });
  
  var keys = lines[0];

  var objects = lines
    .slice(1)
    .map(function(arr) {
      return arr.reduce(function(obj, val, i) {
        obj[keys[i]] = val; 
        return obj;
      }, {});
    });
  
  console.log(objects);
})

This gives me the output in this format:

{
    "PROBLEM_NUMBER": "ticket_number",
    "CALLER_NAME": "\"surname",
    "PRIORITY": " forename\"",
    "CALL_TIME": "4",
    "CALL_DETAILS": "date",
    "RESOLVER": "group",
    "RESTORING_GROUP": "\"surname",
    "RESOLVING_GROUP": " forename\"",
    "RESTORATION_TIME": "group",
    "RAG_STATUS": "group",
    "CALL_STATUS": "date",
    "CALL_TYPE": "RED",
    "RESTORATION_CODE": "Closed",
    "SUBMITTER_GROUP": "Problem",
    "ASSIGNEE_GROUP": "resolution",
    "ASSIGNEE_NAME": "group",
    "RESOLVED_DATE_TIME": "group",
    "RESTORED_DATE_TIME": "",
    "TIME_WITH_TEAM": "date",
    "MONTH/YEAR\r": "date",
    "undefined": "Jan-21\r"
}

As you can see, the final field is "undefined" due to 2 fields containing a comma and splitting incorrectly.

I know I need to use regex to modify the split correctly however I don't understand or know where to put it. Is anyone able to assist me please?

Thanks

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

0

I managed to solve the problem so thought I would post here for anyone else that may come across a similar issue:

To resolve it I declared the regex string as a variable and then called that in the .split() instruction.

The regex string I used was - /("[^"]+"|[^,]+)*,/g

My code now looks like this:

$.get("export.csv", function process(dataString) {
    var regex = /("[^"]+"|[^,]+)*,/g;
    var lines = dataString
    .split(/\n/)
    .map(function(lineStr) {
       return lineStr.split(regex);
    });
  var keys = lines[0];
  var objects = lines
    .slice(1)
    .map(function(arr) {
      return arr.reduce(function(obj, val, i) {
        obj[keys[i]] = val;
        return obj;
      }, {});
    });
  console.log(objects);
})

This gives me the correct output I need and maps all values accordingly

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