What I have:
A CSV file exported from Excel along with double-quotes

  1.  label1,label2 
  2.  item1a,item2a 
  3.  item1c,"item2c,c" 
  4.  item1b,item2b 

What I want:
To read the file (stored on the server) and convert to a JavaScript array of objects
  1.  var my_object_array = [ 
  2.       { my_col1_val: 'item1a', my_col2_val: 'item2a' }, 
  3.       { my_col1_val: 'item1b', my_col2_val: 'item2b' }, 
  4.       { my_col1_val: 'item1c', my_col2_val: 'item2c,c' } 
  5.  ]; 
What I want again:
  • Read a CSV file already uploaded with JavaScript
  • Populate a JS array with each row
  • Account for strings containing double-quotes (and commas to ignore)
  • Sort the resulting object array


An article on how to quickly adapt an array code and sort by its values in JavaScript.

I found examples across the web were partial and thought I'd write a complete example here. This is intended to be usable by Google Chrome web browsers as well as MS IE and Mozilla Firefox browsers. I wanted to:

  1. Sort the array by its values
    1.  my_records.sort(); 
  2. Iterate through and output these in HTML
    1.  for(i=0;i<my_records.length;i++){ 
    2.          // do something to my_records[i] 
    3.  } 

What I have:
  1.  var my_records = new Array(); 
  2.  my_records["item1"] = "My Value 2"; 
  3.  my_records["item2"] = "My Value 3"; 
  4.  my_records["item3"] = "My Value 1"; 
  6.  document.write('<option value="item1">My Value 2</option>'); 
  7.  document.write('<option value="item2">My Value 3</option>'); 
  8.  document.write('<option value="item3">My Value 1</option>'); 

This is to describe how to change the value of a field of a form from it's default when it is clicked on so that it auto-clears and changes to how it normally works.

Note the value in the below field. Now click on it and it disappears, allowing you to type normally. Clear whatever you type so the field is empty and click somewhere on the page (not on a link as this will take you to another page) and the field returns to displaying "Name:".

I've noticed a lot of solutions now use JQuery or Mootools but most of these will not work in Internet Explorer 7 which is unfortunately still in use. I needed a back-to-basics solution and this is the one I have used since my fad of JavaScript 1.2 in the 90s. For broader compatibility, this is my recommended solution.