Another method is to take the CSV data and populate it into a JavaScript object, using the data from the header of the CSV (first row) as the key values.
For example, the code below will pull in the first row as header information, and use each value of the header as the key for the JavaScript object. It will then populate each row into an array in that object, with each item in the array being associated with the key.
Thus, if your header row was "PatientID, LastName, FirstName", you could access PatientID by using the following syntax: row[1].PatientID. Or access the LastName field using this syntax: row[27].LastName. And so on.
Code:
// Initialize variables
var row = [{}];
var i, x, curRow, curCol = 0;
// Use the first row of the CSV as a header row
var headerRow = source.getNode('*[1]');
// Find the number of rows
var numRows = source.getRowCount();
// Let's assign the values from all rows to the array.
// First, we start by expanding our array to have the same
// number of rows as the CSV
for (i = 0; i < numRows-1; i++){
row.push( [] );
}
// Next we populate the array.
// NOTE: our array starts at 1, not 0!
// Access the first row like this: data[1].key
for (i = 1; i < numRows; i++){
for (x = 0; x < headers.length; x++){
curCol = x + 1;
curRow = i + 1;
row[i][headers[x]] = source.getNode(curCol + '[' + curRow + ']').replace(" ","");
}
}