user3172841
user3172841

Reputation: 73

Return a specific line from a csv file using the data.split function in jquery

I am using the following code to get contents from a csv file

$.get(file , function(data) {

  var lines = data.split('\n');

  $.each(lines, function (lineNo, line) {
    var items = line.split(',');
    // MORE STUFF
  });
});

The above code gives me all the lines that are available in my csv file. Here is an example of the data returned

one,0,0,
two,0,0
three,0,0

What i would like is to retrieve only a specific line from the file. for example "two,0,0"

How do i acheive this ?

Thanks

Upvotes: 1

Views: 615

Answers (1)

Jage
Jage

Reputation: 8086

Once you split a string, the result is a numeric array with each portion of the string. So all of your lines are now numbered like any other numeric array. If you wanted the second line, you would just use the key to ask for it:

var lines = data.split("\n");
var secondLine = lines[1];

Upvotes: 1

Related Questions