Reputation: 23
I have an object, and I want to log the values
array but when I do so, the array is empty. Why is that?
var data = {"values" : []};
Papa.parse('data.csv', {
header: true,
download: true,
newline: "\n",
quoteChar : '',
escapeChar : '',
chunk: function(results) {
data.values.push(results.data);
},
});
console.log(data);
console.log(data.values.length); // 0
console.log(data.values[0]); // undefined
Upvotes: 2
Views: 77
Reputation: 370659
Papa.parse
is asynchronous; currently, you're logging the data only after you've send the command to parse the CSV, but the response hasn't come back yet; the callback hasn't triggered. you need to add a complete
handler as described in the docs.
Papa.parse('data.csv', {
header: true,
download: true,
newline: "\n",
quoteChar : '',
escapeChar : '',
chunk: function(results) {
data.values.push(results.data);
},
complete: function() {
console.log('done');
console.log(data.values[0]);
}
});
Upvotes: 2