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
CertainPerformance
374k55 gold badges354 silver badges359 bronze badges
-
3You do not have a JSON there. You have an object. JSON is a method of formatting a string to represent an object.CertainPerformance– CertainPerformance2018年05月12日 23:21:40 +00:00Commented May 12, 2018 at 23:21
-
3Post the (fuller, reproducible) code in question. Sounds like you're not waiting for the object to be populated before you're trying to log it.CertainPerformance– CertainPerformance2018年05月12日 23:22:31 +00:00Commented May 12, 2018 at 23:22
-
thanks for the reply : I already get that "data" is an object and not a String, maybe i'm wasn't exact when i talked about itFlorent Durécu– Florent Durécu2018年05月12日 23:26:19 +00:00Commented May 12, 2018 at 23:26
-
What point in the script are you invoking console.log(); ?user3978398– user39783982018年05月12日 23:26:45 +00:00Commented May 12, 2018 at 23:26
-
it's just to know what happened actually, it wont be there when my work will be overFlorent Durécu– Florent Durécu2018年05月12日 23:33:44 +00:00Commented May 12, 2018 at 23:33
1 Answer 1
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]);
}
});
answered May 12, 2018 at 23:54
CertainPerformance
374k55 gold badges354 silver badges359 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Florent Durécu
You are my savior buddy, that was totally that problem, didn't think of that.
lang-js