2

I have a csv file with headers that look like this

header1,header2,header3,header4
value1,value2,value3,value4
value1,value2,value3,value4
value1,value2,value3,value4

I just want to read the headers and I've tried.

var keys = [];
        for (var k in unemployment2) keys.push(k);

        alert("total " + keys.length + " keys: " + keys);

but I am getting the row numbers instead.

2
  • 1
    I think it needs to be keys.push(unemployment2[k]), that will give you the value of the key-value pair, instead of the key Commented Mar 11, 2014 at 17:17
  • Reading the MDN documentation about for...in should solve your problem. And then you will hopefully see that you should better use a for loop. Commented Mar 11, 2014 at 17:21

2 Answers 2

1

So assuming your csv data looking like this:

heading1,heading2,heading3,heading4,heading5,value1_1,value2_1,value3_1,value4_1,value5_1,value1_2,value2_2,value3_2,value4_2,value5_2....

This will read the data and convert an array like this:

[heading1:value1_1 , heading2:value2_1, heading3 : value3_1, heading4 : value4_1, heading5 : value5_1 ],[heading1:value1_2 , heading2:value2_2, heading3 : value3_2, heading4 : value4_2, heading5 : value5_2 ]....

This code will work when your data.txt file is one long string of comma-separated entries, with no newlines:

data.txt:

heading1,heading2,heading3,heading4,heading5,value1_1,...,value5_2

javascript:

$(document).ready(function() {
    $.ajax({
        type: "GET",
        url: "data.txt",
        dataType: "text",
        success: function(data) {processData(data);}
     });
});

function processData(allText) {
    var record_num = 5;  // or however many elements there are in each row
    var allTextLines = allText.split(/\r\n|\n/);
    var entries = allTextLines[0].split(',');
    var lines = [];

    var headings = entries.splice(0,record_num);
        while (entries.length>0) {
        var tarr = [];
        for (var j=0; j<record_num; j++) {
            tarr.push(headings[j]+":"+entries.shift());
        }
        lines.push(tarr);
    }
    // alert(lines);
}

The following code will work on a "true" CSV file with linebreaks between each set of records:

data.txt:

heading1,heading2,heading3,heading4,heading5 value1_1,value2_1,value3_1,value4_1,value5_1 value1_2,value2_2,value3_2,value4_2,value5_2 javascript:

$(document).ready(function() {
    $.ajax({
        type: "GET",
       url: "data.txt",
        dataType: "text",
        success: function(data) {processData(data);}
     });
});

function processData(allText) {
    var allTextLines = allText.split(/\r\n|\n/);
    var headers = allTextLines[0].split(',');
    var lines = [];

    for (var i=1; i<allTextLines.length; i++) {
        var data = allTextLines[i].split(',');
        if (data.length == headers.length) {

            var tarr = [];
            for (var j=0; j<headers.length; j++) {
                tarr.push(headers[j]+":"+data[j]);
            }
            lines.push(tarr);
        }
    }
    // alert(lines);
   }

http://jsfiddle.net/mblase75/dcqxr/

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

Comments

1

I used the code below because I wanted to stick to straight JS and my CVS was already neatly formatted so I didn't need additional regular expressions to format them. This was a first step to getting the headers from my csv file, so that I can compare it to another variable in my code.

for (key in unemployment2[0]) {
            console.log(key);
        }

1 Comment

Please add some explanation to your answer, and format it better. At this point, this isn't contributing much.

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.