0

I have a csv file which contains the following values:

18/10/2013,  news item 1
18/10/2013,  news item 2
17/10/2013,  news item 3
16/10/2013,  news item 4

How do I go about putting this into an array in JavaScript, ordered by date?

Once I have got it into an array, I also need to get the text values.

So far I have something like this:

Function readTextFile(){

var rawFile = new XMLhttpRequest();
Var myArray;

rawFile.open("GET", csvfile, true);
rawFile.onreadystatechange = function(){

if(rawFile.readyState === 4){
  if(rawFile.Status === 200 || rawFile.Status === 0)
{

}
}
}

Sorry if the text above is not formatted properly, I am posting from my phone. thanks

1

2 Answers 2

2

This is how you can do it.

Function readTextFile(){

var rawFile = new XMLhttpRequest();
Var myArray;

rawFile.open("GET", csvfile, true);
rawFile.onreadystatechange = function(){

if(rawFile.readyState === 4){
  if(rawFile.Status === 200 || rawFile.Status === 0)
{
    var response = rawFile.responseText;
    var splitData = new Array();
     //split data with new line

    splitData = response.split("\n");    //stores all the values separated by new line
    console.log(splitData[0]);      //returns   18/10/2013,  news item 1

  //split single line data with comma 

    var splitComma = new Array();
    var splitComma = splitData[0].split(",");  
    console.log(splitComma[0]);          //returns  18/10/2013

   //start comparing date values here

}
}
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this is what I needed. I appreciate your help
0
var myArray = rawFile.responseText.split(",");

But it will not sort the data according to login date

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.