admin管理员组文章数量:1022935
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.
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.
Share Improve this question edited Mar 11, 2014 at 17:17 Ethan Brown 27.3k5 gold badges84 silver badges94 bronze badges asked Mar 11, 2014 at 17:16 B TB T 1612 gold badges6 silver badges15 bronze badges 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 – Ted 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 afor
loop. – Felix Kling Commented Mar 11, 2014 at 17:21
2 Answers
Reset to default 1So 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 ma-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/mblase75/dcqxr/
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 pare it to another variable in my code.
for (key in unemployment2[0]) {
console.log(key);
}
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.
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.
Share Improve this question edited Mar 11, 2014 at 17:17 Ethan Brown 27.3k5 gold badges84 silver badges94 bronze badges asked Mar 11, 2014 at 17:16 B TB T 1612 gold badges6 silver badges15 bronze badges 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 – Ted 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 afor
loop. – Felix Kling Commented Mar 11, 2014 at 17:21
2 Answers
Reset to default 1So 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 ma-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/mblase75/dcqxr/
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 pare it to another variable in my code.
for (key in unemployment2[0]) {
console.log(key);
}
本文标签: Read CSV headers using JavascriptStack Overflow
版权声明:本文标题:Read CSV headers using Javascript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745492717a2153032.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论