Reputation: 153
Is it possible to convert csv data with this format:
date,value1,value2,value3//CSV header
2015-11-04,58,221,12//second row csv
2015-11-05,62,234,18
2015-11-06,69,258,22
...
to this format?
[12,18,22,...]//Get the data from value3 field
The web references I have seen converts csv file to array, but not into the same format i have written above.
Thanks in advance.
Upvotes: 0
Views: 1136
Reputation: 1506
var rows = data.split("\n");
var array = [];
var final = [];
$.get("csvstring.php", function(data) {
$.each(rows, function(index, value){
if(index > 0) {
var values = value.split(",");
array.push(values);
}
});
$.each(array, function(index, value){
$.each(value, function(indexs, values) {
console.log(index);
if(indexs == 3) {
final.push(values);
}
});
});
});
console.log(final);
This returns:
["12", "18", "22"]
I hope it helped!
Upvotes: 1
Reputation: 669
You can load it by using $.get and parse it using .split(), here's a little example.
$.get("YOUR.csv", function(data){
var yourParsedCSV = data.split("\n").map(function(i){return i.split(",")});
});
Heres what it does, explained
1º split rows with
data.split("\n");
2º loop rows and return them as arrays splitting by commas ( .map is the fastest option)
...map(function(i){ //for every item in the previous array...
return i.split(","); //return it as array
});
Upvotes: 0