0

I was reluctant to use Papa Parse, but now I realize how powerful it is. I am using Papa Parse on a local file, but I don't know how to use the results. I want to be able to use the results so I can combine the array with another and then sort highest to lowest based on a certain element. Console.log doesn't work. From what I have researched, it may have something to do with a callback function. I am stuck on how to do the callback function with Papa Parse. Thanks for any advice.

This is my output

Finished input (async).
Time: 43.90000000000873 
Arguments(3)
0: 
  data: 
    Array(1136) [0 … 99] 
      0: (9) [
        "CONTENT TYPE", "TITLE", "ABBR", "ISSN",
        "e-ISSN", "PUBLICATION RANGE: START",
        "PUBLICATION RANGE: LATEST PUBLISHED",
        "SHORTCUT URL", "ARCHIVE URL"
      ]
      1: (9) [
        "Journals", "ACM Computing Surveys ",
        "ACM Comput. Surv.", "0360-0300", "1557-7341",
        "Volume 1 Issue 1 (March 1969)",
        "Volume 46 Issue 1 (October 2013)", 
        "http://dl.acm.org/citation.cfm?id=J204", 
        "http://dl.acm.org/citation.cfm?id=J204&picked=prox"
      ]
AnonymousSB
  • 3,516
  • 10
  • 28
Dataman
  • 11
  • 9
  • 1
    Could you show what you have tried? A [Minimal, Complete, Verifiable Example](https://stackoverflow.com/help/mcve) would help. – pmkro Nov 29 '18 at 23:39
  • do you have test code? – Eric Nov 29 '18 at 23:40
  • It would be important to note if you're using this in NodeJS, or in the browser. As well as show your `Papa.parse()` call. Just saying `console.log` doesn't work, doesn't tell us what you were trying to log, or where you were trying to log it. – AnonymousSB Nov 30 '18 at 00:10
  • I do not know how to format the code output up here yet. – Dataman Dec 01 '18 at 22:46
  • `Finished input (async). Time: 43.90000000000873 Arguments(3) 0: data: Array(1136) [0 … 99] 0: (9) ["CONTENT TYPE", "TITLE", "ABBR", "ISSN", "e-ISSN", "PUBLICATION RANGE: START", "PUBLICATION RANGE: LATEST PUBLISHED", "SHORTCUT URL", "ARCHIVE URL"] 1: (9) ["Journals", "ACM Computing Surveys ", "ACM Comput. Surv.", "0360-0300", "1557-7341", "Volume 1 Issue 1 (March 1969)", "Volume 46 Issue 1 (October 2013)", "http://dl.acm.org/citation.cfm?id=J204", "http://dl.acm.org/citation.cfm?id=J204&picked=prox"] ` ​ – Dataman Dec 01 '18 at 23:11
  • It's good to see the output, but we really need to see the code that lead to that output. – AnonymousSB Dec 02 '18 at 00:01
  • If you download PapaParse and unzip the files, there is a player html file in there. It looks very similar to the demo on the site. I select the file I want to parse and then the results show in the javascript console panel. I don't know what to do with the results. – Dataman Dec 02 '18 at 02:03
  • What are you hoping to do? Do you want to just use the demo to turn CSV into an array? Or do you want to use Papa Parse on a website where you can select a file? The output Papa Parse is giving you in the demo is just for demonstration purposes, the output in the console is formatted to be readable, not usable. – AnonymousSB Dec 02 '18 at 07:58
  • Please take a look at my updated answer with a 100% functional example. Just click "Run code snippet" and select a file. – AnonymousSB Dec 02 '18 at 08:26
  • I want to turn the CSV file into a javascript array and then do something with it such as sort the data or combine it with another array. It is a local CSV file. It is not on a file on a web page server. – Dataman Dec 02 '18 at 12:36

1 Answers1

0

Based on a conversation with you, it appears you're trying to retrofit the Papa Parse demo for your own needs. Below is a stripped down code snippet that should be drop-in-ready for your project and will get you started.

document.addEventListener('DOMContentLoaded', () => {
    const file = document.getElementById('file');
    
    file.addEventListener('change', () => {
        Papa.parse(file.files[0], {
            complete: function(results) {

                // Here you can do something with results.data
                console.log("Finished:", results.data);

            }
        });
    })
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/4.6.2/papaparse.js"></script>
<input type="file" id="file" />

Original Answer

Since I suspect you're loading your local csv file from the files system, and not an upload form, you'll need to use download: true to make it work.

Papa.parse('data.csv', {
  download: true,
  complete: function(results) {
    console.log("Finished:", results.data);
  }
});

Technically, when loading local files, you're supposed to supply Papa.parse with a File Object. This is a snippet from MDN File API documentation

File objects are generally retrieved from a FileList object returned as a result of a user selecting files using the input element

If of course you're running this in NodeJS, then you'd just do the following:

const fs = require('fs');
const Papa = require('papaparse');
const csv = fs.createReadStream('data.csv');

Papa.parse(csv, {
    complete: function(results) {
        console.log("Finished:", results);
    }
});

Documentation

https://www.papaparse.com/docs#local-files

https://developer.mozilla.org/en-US/docs/Web/API/File

AnonymousSB
  • 3,516
  • 10
  • 28
  • You paste your code into the field, highlight just the code and press the `{ }` button. It'll shift your code over 4 spaces, which is automatically recognized as code. – AnonymousSB Dec 01 '18 at 23:55