šŸ  back to Observable

Import CSV and add index to rows

I wonder if it is at all possible when importing a csv file to have the ability to add an index value? or am I missing something ā€¦

When I import a csv in Observable I get an Array of objects, I then slice and filter the data in different ways, but when filtering I would like to keep a record of the index so I can flag the rows in the original dataset, hope that makes sense.

For example, one of my filters goes like this:

{
  const byAddress = d => d['Address1']
  const arrs = [data.map(byAddress), uniques.map(byAddress)];
  return arrs.reduce((a, b) => a.filter(c => !b.includes(c)));
}

and it gives an Array with the filtered data Iā€™m looking for but I would like to know the index of each row so I can correlate to the original data.

Do you mean like this added the index of the map?

const arrs = [data.map(byAddress,index), uniques.map(byAddress)];

Like so?

dataWithIndex = data.map((d, i) => ({...d, index: i}))

Or together when you load a CSV:

data = {
  const data = await FileAttachment("data.csv").csv({typed: true});
  return data.map((d, i) => ({...d, index: i}));
}
2 Likes