0

After browsing Stack Overflow for a solution, I'm stuck on how to proceed. I am using a website to build a model of something and need to be able to read a CSV file laid out as shown below:

Variable Name 1, Value 1
Variable Name 2, Value 2
Variable Name 3, Value 3

The main issue is that the website only supports Javascript and their own API linked below: https://insightmaker.com/sites/default/files/API/files/API-js.html

The code that I am trying to write needs to be able to put the values into two separate arrays or one if need be and then read the variable name and the value and make changes.

Is this in any way possible without using separate JS plug-ins?

Any help would be greatly appreciated.

Thanks in advance

Edit:

The code that I have written so far looks like:

// This function comes from the API that Insight Maker Provides
openFile({
    read: "Text",
    multiple: false,
    onCompleted: function(result,allText){storeData(result,allText);}
});


// The Main function of the code
function storeData(result,allText){
    alert(result.contents);
    var name = [];
    var value = [];
    var txt = "";
    var space = ",";
    var newline = "\n";
    //var reader = new FileReader();

}

So far, the code that I have written successfully manages to output the full contents of the CSV file that is selected from the dialog box in an alert box.

What I am trying to do is to take the data from the CSV file and place it into an array.

An exaple of the code i'm trying to write is like this c++ code and find a substitute for this kind of code in javascript:

    ifstream input("input.txt");
    for (i = 0; i < 3; i++)
    {
        getline(input, text);
        a[i] = text;
        cout << text << endl;
    }
a_chr1s
  • 25
  • 4
  • You have two questions. The first is read a CSV from JS, then I'd suggest you to check this: https://stackoverflow.com/questions/7431268/how-to-read-data-from-csv-file-using-javascript The second is an independant issue. – Pac0 Aug 11 '20 at 13:10
  • Please provide more detail on what you have tried, what the output is, and what the expected output would be. – ChrisG Aug 11 '20 at 13:34
  • In the example, do you mean something like `VarName, someValue / OtherName, OtherVal`? – VeryGoodDog Aug 11 '20 at 14:14
  • @ChrisG I have added some detail to the post about what I have tried. Hopefully this helps – a_chr1s Aug 11 '20 at 14:31
  • @VeryGoodDog Yes. Essentially you have variables ```A, B, C``` and they have the values ```1, 2, 3``` and in the csv file they are laid out like ```A, 1; B, 2 ;C, 3``` – a_chr1s Aug 11 '20 at 14:37

1 Answers1

0

This can easily be done with String.prototype.split(), String.prototype.match(), and Array.prototype.forEach() (or some equivalent loop if .forEach isn't available).

// First, we have the string and we must split it on any newlines and semicolons.
// This can be done with a regex.
// Assuming csv holds the csv string.
var csvRows = csv.split(/;|\n/); // split on semicolons or newlines

// Now we create the arrays that will hold the name and values.
var varNames = [];
var varVals = [];

// Then iterate over all the csv rows, "row" is each split row
csvRows.forEach(row => {
    // matches everything before the comma (variable name)
    // then the comma (and an optional space)
    // then finally everything after the comma (variable value)
    var rowParts = row.match(/(.+),\s?(.+)/);
    // push adds the element to the end of the array
    // rowParts[0] is the whole match so rowParts[1] is the varName
    varNames.push(rowParts[1]);
    varVals.push(rowParts[2]);
});

This could also be a for (var i = 0; i < csvRows.length; i++) but forEach is easier :)

VeryGoodDog
  • 335
  • 2
  • 11
  • Thanks for the response. I have just had time to try your suggestion and seems to work great. Although, I'm getting an error that says: ```Uncaught ReferenceError: csv is not defined : : 28 out9.js:76 ReferenceError: csv is not defined at storeData (eval at runAction (out9.js:238), :28:19) at Object.onCompleted (eval at runAction (out9.js:238), :6:43) at e (out9.js:202) at FileReader.d.onloadend (out9.js:202)``` How would I go sorting out the error or could it be something that could be an issue with the website? – a_chr1s Aug 14 '20 at 15:49
  • that error is being thrown because i assumed you have already read the csv file into a string, stored in `csv`. if you read the file into any other variable, you will need to change the name of `csv` in the first line. – VeryGoodDog Aug 15 '20 at 21:27
  • Ahh. That makes sense now. Thank you very much for the help – a_chr1s Aug 17 '20 at 08:59