I am loading a CSV file that looks like this in JavaScript:
user_id, aligned_audio_onset_sec, x, y
1, 1.3 , 0.3, 0.5
1, 5, 0.9, 0.3
3, 4, 0.5, 0.5
With this script:
$(function() {
var VAdata = $.get("./VAdata.data", function(data) {
  VAdata = data.split(/\r?\n/).map(pair => pair.split(/,/).map(Number));
  $('#log').text(JSON.stringify(VAdata));
  });
});
It works well, except it does not ignore the headers, but I can't figure out how to do that. I was also wondering if I could do a check to see if all rows have a length of 4 and remove those that don't. (I can only think of copying row by row and only including those with full length.)
The log div outputs:
[[null,null,null,null],[1,1.3,0.3,0.5],[1,5,0.9,0.3],[3,4,0.5,0.5],[0]]
                Use Array#filter method for that.
VAdata = data
          .split(/\r?\n/)
          .filter((v, i) => i && v.split(/,/).length >= 4)
          .map(pair => pair.split(/,/)
          .map(Number));
var data = 'user_id, aligned_audio_onset_sec, x, y\n1, 1.3 , 0.3, 0.5\n1, 5, 0.9, 0.3\n3, 4, 0.5, 0.5';
var VAdata = data
  .split(/\r?\n/)
  .filter((v, i) => i && v.split(/,/).length >= 4)
  .map(pair => pair.split(/,/)
    .map(Number));
console.log(VAdata);
Where i && pair.split(/,/).length >= 4 :
i would be falsy when 0 first element would be ignored.v.split(/,/).length >= 4 would help to filter out full length elements.You can use a simple for loop or Array#forEach to reduce the number of iterations.
VAdata = [];
data.split(/\r?\n/)
    .forEach((pair, i) => { 
       let spl = pair.split(/,/);
       i && spl.length >= 4 && VAdata.push(spl.map(Number))
    });
var data = 'user_id, aligned_audio_onset_sec, x, y\n1, 1.3 , 0.3, 0.5\n1, 5, 0.9, 0.3\n3, 4, 0.5, 0.5';
var VAdata = [];
data.split(/\r?\n/)
  .forEach((pair, i) => {
    let spl = pair.split(/,/);
    i && spl.length >= 4 && VAdata.push(spl.map(Number))
  });
console.log(VAdata)
If you want to treat NaN value as 0 then use:
...push(spl.map(v => Number(v) || 0))
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With