2

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
asked May 12, 2018 at 23:20
7
  • 3
    You do not have a JSON there. You have an object. JSON is a method of formatting a string to represent an object. Commented May 12, 2018 at 23:21
  • 3
    Post 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. Commented 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 it Commented May 12, 2018 at 23:26
  • What point in the script are you invoking console.log(); ? Commented May 12, 2018 at 23:26
  • it's just to know what happened actually, it wont be there when my work will be over Commented May 12, 2018 at 23:33

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

You are my savior buddy, that was totally that problem, didn't think of that.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.