Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript data load - how to ignore headers and verify data

Tags:

javascript

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]]
like image 755
dorien Avatar asked Mar 09 '23 16:03

dorien


1 Answers

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 :

  1. i would be falsy when 0 first element would be ignored.
  2. 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))
like image 155
Pranav C Balan Avatar answered Apr 26 '23 01:04

Pranav C Balan