score:1

Accepted answer

That row function (called type) you shared is not removing anything. It's just selecting different columns to add to the total property.

Personally, I'd load the CSV as it is, without deleting anything, and I'd create another array, filtering the original one by column. Another option is defining the columns in the stack generator. In both ways you keep all original columns, in case you need them lather. It's almost never wise deleting information: just choose what information will be used.

However, if you want to do that using the row function, this is how you can do:

function type(d, i, columns) {
  for (var j = 0; j < columns.length; j++) {
    if (j > 1) delete d[columns[j]]
  }
  return d;
}

Here is the demo with your data, check the console:

var csv = `SL,Outpatient,Inpatient,Trend,Threshold
SL 1, 42, 99, -1, 100
SL 2, 23, 58, +1, 100
SL 3, 15, 74, 0, 100
SL 4, 99, 23, +1, 100
SL 5, 33, 86, +1, 100
SL 6, 38, 20, 0, 100`;

var data = d3.csvParse(csv, type);

function type(d, i, columns) {
  for (var j = 0; j < columns.length; j++) {
    if (j > 1) delete d[columns[j]]
  }
  return d;
}

console.log(data)
<script src="https://d3js.org/d3.v4.min.js"></script>

PS: I believe that by "columns 1 and 2 of data" you are referring to "Outpatient" and "Inpatient". In that case, do if (j > 2).


Related Query

More Query from same tag