javascriptjquerycsvjquery-csv

Parsing csv data with jQuery-csv


I am parsing through a csv file. My goal is to look for the names that starts with "A", "B" and count the occurrences. The problem with the following code is it never completes the while loop.

HTML:

<form>
  <input type="button" id="csv1ButtonRun" value="Run" style="background-color: lightgray"/>
</form>

Scripts included:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="app.js"></script>
<script src="jquery.csv-0.71.js"></script>

JavaScript:

var babyCsv = " Jacob, 1, boy, 2010, Isabella, 1, girl, 2010, Ethan, 2, boy, 2010, Sophia, 2, girl, 2010, Michael, 3, boy, 2010, Emma, 3, girl, 2010, Jayden, 4, boy, 2010, Olivia, 4, girl, 2010, William, 5, boy, 2010, Ava, 5, girl, 2010, Alexander, 6, boy, 2010, Emily, 6, girl, 2010, Noah, 7, boy, 2010, Abigail, 7, girl, 2010, Daniel, 8, boy, 2010, Madison, 8, girl, 2010, Aiden, 9, boy, 2010, Chloe, 9, girl, 2010, Anthony, 10, boy, 2010, Mia, 10, girl, 2010, Joshua, 11, boy, 2010, Addison, 11, girl, 2010, Mason, 12, boy, 2010, Elizabeth, 12, girl, 2010, Christopher, 13, boy, 2010, Ella, 13, girl, 2010, Andrew, 14, boy, 2010, Natalie, 14, girl, 2010, David, 15, boy, 2010, Samantha, 15, girl, 2010, Matthew, 16, boy, 2010";

var data = $.csv.toArray(babyCsv);

$("#csv1ButtonRun").click(function() {

    var arrayLength = data.length / 4;
    var count1 = 0;
    var count2 = 0;
    var i = 0;

    while (i <= arrayLength) {

        var name = data[i * 4];
        if (name.substring(0, 2) == " A") {
            count1 = count1 + 1;
        }
        else if (name.substring(0, 2) == " B") {
            count2 = count2 + 1;
        };
        i++;
    };

    document.write(count1 + count2);
});

Solution

  • jsFiddle

    Your CSV data isn't properly formatted, so your JS object doesn't have any meaningful structure to it. You should separate individual records (rows) in the file with newline characters. Use commas to separate values within a single row. Like:

    var babyCsv = " Bryan, 1, boy, 2010 \n Isabella, 1, girl, 2010 \n Bobby, 2, boy, 2010 \n Sophia, 2, girl, 2010 \n Nina, 5, girl, 2010 \n Alexander, 6, boy, 2010 \n Aiden, 9, boy, 2010 \n Anthony, 10, boy, 2010";
    

    Then use the appropriate $.csv.toArrays method to parse the data and return a 2D array of records. Then each entry in your array is for a single child, in the form:

    [
        [name1, age1, gender1, year1],
        [name2, age2, gender2, year2],
        ...
    ];
    

    Finally, in your loop, you must update the value of the name variable each iteration so that you actually do check against each of the children; currently your not changing the value of name between iterations. This is really nicely suited to a for loop. $.trim will also make it easy to get rid of any extra white spaces at the beginnings of strings, though jquery-csv may already do that. So your final code would look like:

    var babyCsv = "..."; // your data here
    
    var data = $.csv.toArrays(babyCsv);
    
    $(function () {
        $("#csv1ButtonRun").click(function () {
    
            var count1 = 0;
            var count2 = 0;
    
            console.log("Num kids: " + data.length);
    
            for (var i = 0; i < data.length; i++) {
                var name = $.trim(data[i][0]); //name is first column
    
                if (name[0] == "A") {
                    count1++;
                } else if (name[0] == "B") {
                    count2++;
                };
            };
    
            console.log("Kids with A or B names: " + (count1 + count2));
        });
    });