1

How can I open a .csv file and turn values into a javascript array.

In classical programming i'd do this by:

  1. opening the file as a string
  2. splitting by ,
  3. close the file

I know how to split, .split(','), but how do I open and close the csv file in javascript or jquery?

gariepy
  • 3,576
  • 6
  • 21
  • 34
MrGuru
  • 325
  • 5
  • 15

1 Answers1

2
$.get(url,function(data)
    {
        var MainArray = data.split('\n');
        for(var i=0;i<MainArray.length;i++)
        {
            MainArray[i] = MainArray[i].split(',');
            // now MainArray is a two dimensional array that contains csv file 
            //MainArray[row index][column index]
        }
    }
);
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
  • what if it is a local .csv file? 'data.csv' is not working for me – MrGuru Jun 08 '14 at 14:11
  • what do you mean by local csv file? if the url of the csv file is correct it will work however if the csv is somewhere that you have permission issue reading it then you won't be able to read it anyway – Ashkan Mobayen Khiabani Jun 08 '14 at 14:14
  • my apologies, it was just not working on my local file. Works fine when online. – MrGuru Jun 08 '14 at 16:41