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?

0

1 Answer 1

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]
        }
    }
);
Sign up to request clarification or add additional context in comments.

3 Comments

what if it is a local .csv file? 'data.csv' is not working for me
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
my apologies, it was just not working on my local file. Works fine when online.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.