Javascript function to not read blank line breaks

0

I'm trying to read a txt file with javascript and convert it to JSON, but the files at the end have a blank line break

  

Example of the TXT file

  • (0, "A": RED, 1)
  • (1, "B": GREEN, 4)
  •  3.

    This is my function

                    function parseContent(content) {
        //Couper chaque ligne du fichier aprés saut de ligne
        var lines = content.split("\n");
        //Array à stocker les donnés
        var result = [];
        //Creation de l'automate
        var automateObject;
        var title;
        var currentline
        //Parcourir tout le fichier
        for (var i = 0; i < lines.length; i++) {
          //Pour creer l'onbjet automate
            if(i == 0){
              currentline = lines[i].split("\n");
              currentline = lines[i].replace(/[\(\)]/g, '');  
              currentline = currentline.replace(/,/g, '');   
              currentline = currentline.split(" ");
              automateObject = new Automate(parseInt(currentline[1]),parseInt(currentline[2]),parseInt(currentline[3]));
            } else {
                //variable pour stocker le titre et la couleur
                var tam;
                //variable pour couper chaque ligne du fichier
                currentline = lines[i].split("\n");
                if(currentline.length > 0){
                  //Enlever les parenthese de la chaine de characters
                  currentline = lines[i].replace(/[\(\)]/g, '');
                  //Enlever les citations de la chaine
                  currentline = currentline.replace(/['"]+/g, '');
                  //Couper la chaine en 3 morceaux
                  currentline = currentline.split(",",3);
                  //Couper le deuxieme atribut pour obtenir le nom de lien et la couleur
                  title = currentline[1].split(":");
                  //Creation d'un object de type automate
                  const etat = new Etat(parseInt(currentline[0]),title[0],title[1],parseInt(currentline[2]));
                  //Ajouter l'object dans l'array
                  result.push(etat);
                } 
            }
        } 
    automateObject.Etats = result;        
          //Conversion de l'array en JSON
    console.log(JSON.stringify(automateObject));
    }
    

    With the length point, I try to avoid reading the blank line break, but the line length does not have a size and it is 1

    How could you say that you do not read the blank line breaks?

        
    asked by Ernesto Emmanuel Yah Lopez 01.03.2018 в 09:29
    source

    2 answers

    3

    You can do this

    currentline = lines[i].trim().split("\n");
    

    Or, if you have a "for" above that you go through the lines, do the TRIM there and that way you take off the checks. That is, lines [i] sure comes from another split ("\ n"). Then put the TRIM there.

    for (i = 0; i < contenido.trim().split('\n').length; i++)
    

    In summary, it is better to exclude that empty line from the beginning and thus minimize errors.

    Edited: put this up.

    var lines = content.trim().split("\n");
    
        
    answered by 01.03.2018 в 09:41
    1

    After trying some of your answers, I can not solve it in that way, so I decided to use a condition every time a line is read

    if(lines[i].length > 1){
    }
    

    and with this I avoid doing the whole process of each line if the line is a blank line

        
    answered by 06.04.2018 в 12:46