Turn a CSV string into a Javascript object

In response to http://stackoverflow.com/questions/15009744/how-to-iterate-over-a-javascript-line-delimited-string-check-the-semi-colon-del#15009744

by Barney Carroll

HTML

<pre class="CSV">
"ID";"Date";"Time";"Status"
"1";"2013.01.01";"10:03 AM";"Active"
"1";"2013.01.05";"07:45 AM";"Active"
"2";"2013.01.03";"9:12 PM";"Active"
"2";"2013.01.11";"6:37 AM";"Inactive"
"1";"2013.01.22";"12:57 PM";"Inactive"
</pre>
<input id="search_box" type="text">
<button id="btn_search">Search</button>
<pre class="output"></pre>

JavaScript

$('#btn_search').on('click', function(e){
    e.preventDefault();
    
    var results = findEntriesWithID($('#search_box').val());
    
    $('.output').text(JSON.stringify(results));
})

var csvString = $.trim($('.CSV').text());
var csvArray = csvToArray(csvString);

function findEntriesWithID(ID){
    var entries = [];
    
    for(var i = 0; i < csvArray.length; ++i){
        var row = csvArray[i];
        
        if(row.ID === ID){
            entries.push(row);
        }
    }
    
    return entries;
}

function csvToArray(csvString){
  // The array we're going to build
  var csvArray   = [];
  // Break it into rows to start
  var csvRows    = csvString.split(/\n/);
  // Take off the first line to get the headers, then split that into an array
  var csvHeaders = csvRows.shift().split(';');

  // Loop through remaining rows
  for(var rowIndex = 0; rowIndex < csvRows.length; ++rowIndex){
    var rowArray  = csvRows[rowIndex].split(';');

    // Create a new row object to store our data.
    var rowObject = csvArray[rowIndex] = {};
    
    // Then iterate through the remaining properties and use the headers as keys
    for(var propIndex = 0; propIndex < rowArray.length; ++propIndex){
      // Grab the value from the row array we're looping through...
      var propValue =   rowArray[propIndex].replace(/^"|"$/g,'');
      // ...also grab the relevant header (the RegExp in both of these removes quotes)
      var propLabel = csvHeaders[propIndex].replace(/^"|"$/g,'');;

      rowObject[propLabel] = propValue;
    }
  }

  return csvArray;
}