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


What I want:
To read the file (stored on the server) and convert to a JavaScript array of objects
var my_object_array = [
     { my_col1_val: 'item1a', my_col2_val: 'item2a' },
     { my_col1_val: 'item1b', my_col2_val: 'item2b' },
     { my_col1_val: 'item1c', my_col2_val: 'item2c,c' }
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
  2. Iterate through and output these in HTML

What I have:
var my_records = new Array();
my_records["item1"] = "My Value 2";
my_records["item2"] = "My Value 3";
my_records["item3"] = "My Value 1";

document.write('<option value="item1">My Value 2</option>');
document.write('<option value="item2">My Value 3</option>');
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.