I have a csv file containing entries as follows:
a,10
b,20
c,30
d,40
In JavaScript, I want to assign this csv file to a variable lets say x as follow:
x = [
['a',10],
['b',20],
['c',30],
['d',40]
]
Can someone tell me how i can do this.
I have a csv file containing entries as follows:
a,10
b,20
c,30
d,40
In JavaScript, I want to assign this csv file to a variable lets say x as follow:
x = [
['a',10],
['b',20],
['c',30],
['d',40]
]
Can someone tell me how i can do this.
If your CSV is really that simple, you just need two splits and a loop:
var rows = "a,10\nb,20\nc,30\nd,40".split('\n');
var x = [];
for(var i=0; i<rows.length; i++) {
x.push(rows.split(','));
}
A shorter version:
var x = "a,10\nb,20\nc,30\nd,40".split('\n').map(function(val) {
return val.split(',');
});