How can i iterate through columns in an CSV file?


I need to do some calculations and i need to iterate through the columns in st_teff so i can compare them and do some calculations. But i haven’t found anything that helps me go through each element in the column.

for (const planets in newDataPlanetas) {
  console.log(`${planets.pl_name}: ${planets[st_teff]+}`);
}

Note the + on planets[st_teff]+ that converts the string into a number.
I could also be written as planets.st_teff+

let results = []
for (const planets in newDataPlanetas) {
  console.log(`${planets.pl_name}: ${planets[st_teff]+}`);
 results.push( (planets.st_teff+)/3.142 )
}

This 2nd example will do some math and store a result in an array. It will have the same index value of newDataPlanetas for easy cross-referencing

For me to do some math i have to change the values in the st_teff column to numbers, store them in an array?
So inside this loop i can do some calculations like this:

function getArray(newDataPlanetas) {
  let result = []
  const transformedArray = [];
for(const planets in newDataPlanetas){
  
    console.log(`${planets.pl_name}: ${planets[st_teff]+}`);
    results.push( (planets.st_teff+)
  
  for (const row of newDataPlanetas) {
    const transformedRow = {};
    for (const column of columnsToKeep) {
      transformedRow[column] = row[column];
    }    
      
      if(results < 3500){
        transformedArray.push(Object.assign({}, transformedRow, {bolcon: -2.0}));
      }
      
  }

}
  return transformedArray;
}

In here i’m adding a new column called bolcons that depending the temperature the number of the bolcon is assign. There are more if and else but to test it i only added one and i get an error in:

console.log(`${planets.pl_name}: ${planets[st_teff]+}`);

Here is the publish link:

So sorry, I should have check my code as I have got so many bugs in it.

result = {
  let results = []
  for (const planet in newDataPlanetas) {
    results.push( +newDataPlanetas[planet].st_teff )
  }
  return results
}

But I should have also used for of like you did.

In observable each cell is like a function, so the getArray function you wrote can be written like this.

starTempMagDist = {
  const transformedArray = [];

  for (const row of newDataPlanetas) {
    const transformedRow = {};
    for (const column of columnsToKeep) {
      transformedRow[column] = row[column];
    }    

    if(row.st_teff < 3500){
      transformedArray.push(Object.assign({}, transformedRow, {bolcon: -2.0}));
    }

  }
  return transformedArray;
}

Note that you don’t need to call the function to set the variable starTempMagDist

Also in observable you can parse a text file as a csv directly into a json object.

dataPlanetas = FileAttachment("dataPlanetas.csv").csv()

instead of dataPlanetas = d3.csvParse(await file.text())

or more condensed

dataPlanetas1b = (await FileAttachment("dataPlanetas.csv").csv()).filter (d => d.st_teff)

And this might be a more readable way to construct the object:

starTempMagDist = {
  const transformedArray = [];

  for (const row of newDataPlanetas) {
    // const transformedRow = {};
    // for (const column of columnsToKeep) {
    //   transformedRow[column] = row[column];
    // }    

    if(row.st_teff < 3500){
      //  transformedArray.push(Object.assign({}, transformedRow, {bolcon: -2.0}));

      transformedArray.push( {st_teff: +row.st_teff, 
                              sy_vmag: +row.sy_vmag, 
                              sy_dist: +row.sy_dist, 
                              spectClass: setSpecTypes(+row.sy_vmag), 
                              bolcon: -2.0 })
    }

  }

  return transformedArray;
}

Thanks for the help!
When i’m trying to add the bolcon and spect class any st_teff under 5000 and bigger then 3500 doesn’t get added to the array. Any idea why? I think it’s in the else if && line.

else if (row.steff > 3500 **&&** row.st_teff < 5000)
getSpectBolconAbsMagArray = {
  const transformedArray = [];

  for (const row of newDataPlanetas) {
    // const transformedRow = {};
    // for (const column of columnsToKeep) {
    //   transformedRow[column] = row[column];
    // }    

    if(row.st_teff < 3500){
      //  transformedArray.push(Object.assign({}, transformedRow, {bolcon: -2.0}));

      transformedArray.push( {st_teff: +row.st_teff, 
                              sy_vmag: +row.sy_vmag, 
                              sy_dist: +row.sy_dist, 
                              spectClass: 'M', 
                              bolcon: -2.0, 
                              absMag: row.sy_vmag - 5*Math.log10(row.sy_dist/10)})
    }
    else if (row.steff > 3500 && row.st_teff < 5000){

       transformedArray.push( {st_teff: +row.st_teff, 
                              sy_vmag: +row.sy_vmag, 
                              sy_dist: +row.sy_dist, 
                              spectClass: "K", 
                              bolcon: -2.0,
                              absMag: row.sy_vmag - 5*Math.log10(row.sy_dist/10)})

    }
    else if (row.st_teff > 5000 && row.st_teff < 6000){

        transformedArray.push( {st_teff: +row.st_teff, 
                              sy_vmag: +row.sy_vmag, 
                              sy_dist: +row.sy_dist, 
                              spectClass: "G", 
                              bolcon: -0.4,
                               absMag: row.sy_vmag - 5*Math.log10(row.sy_dist/10)})
   }
    else if (row.st_teff > 6000 && row.st_teff < 7500){

        transformedArray.push( {st_teff: +row.st_teff, 
                              sy_vmag: +row.sy_vmag, 
                              sy_dist: +row.sy_dist, 
                              spectClass: "F", 
                              bolcon: -0.15,
                               absMag: row.sy_vmag - 5*Math.log10(row.sy_dist/10)})
   }
    else if (row.st_teff > 7500 && row.st_teff < 10000){

        transformedArray.push( {st_teff: +row.st_teff, 
                              sy_vmag: +row.sy_vmag, 
                              sy_dist: +row.sy_dist, 
                              spectClass: "A", 
                              bolcon: -0.2,
                               absMag: row.sy_vmag - 5*Math.log10(row.sy_dist/10)})
   }
      else if (row.st_teff > 10000 && row.st_teff < 25000){

        transformedArray.push( {st_teff: +row.st_teff, 
                              sy_vmag: +row.sy_vmag, 
                              sy_dist: +row.sy_dist, 
                              spectClass: "B", 
                              bolcon: -2.0,
                              absMag: row.sy_vmag - 5*Math.log10(row.sy_dist/10)})
   }
    else{
       transformedArray.push( {st_teff: +row.st_teff, 
                              sy_vmag: +row.sy_vmag, 
                              sy_dist: +row.sy_dist, 
                              spectClass: 0, 
                              bolcon: 0 })
    }

  }  

  return transformedArray;
}

Typo’s row.st_teff
You have else if (row.steff > 3500 && row.st_teff < 5000){
not else if (row.st_teff > 3500 && row.st_teff < 5000){