0

The function below parses a csv file into an array so it looks something like

[[1,56.79],[2,57.50],[3,71.60],[4,69.10]]

function CSVToArray (csvString) {

    var arrData = [[]];

    var rows = csvString.match(/[^\r\n]+/g);
    var len = rows.length
    for (var i=0; i<len; ++i) 
    {

        var cols = rows[i].split(",")
        arrData[ arrData.length - 1 ].push( parseFloat(cols[0]) );
        arrData[ arrData.length - 1 ].push( parseFloat(cols[1]) );
        if (i<(len-1))
        {
            arrData.push( [] );
        }
    }

    return( arrData );
};

I'm attempting to amend it so that I can have nulls in the array if there is no data similar to

[[1,56.79],[2,57.50],null,[3,71.60],null,[4,69.10]]

I've tried the following but cannot get it to work properly

function CSVToArray (csvString) {

    var arrData = [[]];

    var rows = csvString.match(/[^\r\n]+/g);
    var len = rows.length
    for (var i=0; i<len; ++i) 
    {

        var cols = rows[i].split(",")
        if (cols[1] != "")
        {
        arrData[ arrData.length - 1 ].push( parseFloat(cols[0]) );
        arrData[ arrData.length - 1 ].push( parseFloat(cols[1]) );
        arrData.push( [] );
        }
        else
        {
        arrData.push( null);
        }
    }

    return( arrData );
};

Please can someone advise how I get a null rather than an null inside an array?

Essentially I'm trying to get the data structure right to be able to do this http://jsfiddle.net/KhpCE/1/

2
  • You mean replacing all empty strings in the array by null? Commented Oct 27, 2012 at 18:26
  • In your code you are checking cols[1] != "", but you have accepted an answer that instead checks for strings that doesn't have two columns, i.e. where cols[1] doesn't even exist. So, the lines where there is no data, what do they look like? Commented Oct 27, 2012 at 22:08

3 Answers 3

2

Use this:

function CSVToArray (csvString) {
    var arrData = [];
    var rows = csvString.split(/\r?\n/);
    // console.log(rows)
    var len = rows.length
    for (var i=0; i<len; ++i) 
    {
        var cols = rows[i].split(",")
        // console.log(cols)
        var temp = []
        if (cols.length == 2 && cols[1] != "" && cols[0] != "")
        {
          temp.push( parseFloat(cols[0]) );
          temp.push( parseFloat(cols[1]) );
        }
        else
        {
          temp = null;
        }
        arrData.push(temp);
    }
    return( arrData );
};
Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't solve the problem. Try it with '1,2.1\n\n23.2\n3,4\n5.34,6' It should return an array with 5 elements, and the second and third elements should be null
i thought those cases were invalid inputs, fixed it now
1

It's the match that's bothering you. The result of matching on /[^\r\n]+/g looses empty lines from csvString. I would advise using split here.

Furthermore your code looks pretty bulky. How about using Array.map1:

function CSVToArray(csvString) {
    var rows = csvString.split(/\n|\r\n/);
      return rows.length ? [rows.map(function(s){
         var cols = s.split(",").map(function(v){return Number(v)||null;});
         return cols.length===2 ? cols: null;
      })] : [];
};
//usage example
CSVToArray('1,2.1\n\n23.2\n3,4\n5.34,6');
//=> [[2,2.1],null,null,[3,4],[5.34,6]]

1 Here you can find an Array.map shim for older browsers

1 Comment

If there were empty strings in the CSV, then checking for cols[1] != "" doesn't make sense...
1

Instead of pushing an empty array for the next item beforehand, just create an array of each item and push that, then it's simple to push a null value instead of an item:

function CSVToArray (csvString) {

  // an array without the prefilled empty item array
  var arrData = [];

  var rows = csvString.match(/[^\r\n]+/g);
  var len = rows.length
  for (var i=0; i<len; ++i) {
    var cols = rows[i].split(",");
    if (cols[1] != "") {
      // create an array and push it
      arrData.push([
        parseFloat(cols[0]),
        parseFloat(cols[1])
      ]);
    } else {
      arrData.push(null);
    }
  }

  return arrData;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.