I am trying to output a multi-dimensional array to a CSV file, where each array outputs as a column in the file. Here are my declarations for the arrays:
thetaColumn =
[-10,-9.95,-9.9,-9.85,-9.8,-9.75,-9.7,-9.65,-9.6,-9.55,-9.5,-9.45,-9.4,-9.35,-9.3,-9.25,-9.2,-9.15,-9.1,-9.05,-9,-8.95,-8.9,-8.85,-8.8,-8.75,-8.7,-8.65,-8.6,-8.55,-8.5,-8.45,-8.4,-8.35,-8.3,-8.25,-8.2,-8.15,-8.1,-8.05,-8,-7.95,-7.9,-7.85,-7.8,-7.75,-7.7,-7.65,-7.6,-7.55,-7.5,-7.45,-7.4,-7.35,-7.3,-7.25,-7.2,-7.15,-7.1,-7.05,-7,-6.95,-6.9,-6.85,-6.8,-6.75,-6.7,-6.65,-6.6,-6.55,-6.5,-6.45,-6.4,-6.35,-6.3,-6.25,-6.2,-6.15,-6.1,-6.05,-6,-5.95,-5.9,-5.85,-5.8,-5.75,-5.7,-5.65,-5.6,-5.55,-5.5,-5.45,-5.4,-5.35,-5.3,-5.25,-5.2,-5.15,-5.1,-5.05,-5,-4.95,-4.9,-4.85,-4.8,-4.75,-4.7,-4.65,-4.6,-4.55,-4.5,-4.45,-4.4,-4.35,-4.3,-4.25,-4.2,-4.15,-4.1,-4.05,-4,-3.95,-3.9,-3.85,-3.8,-3.75,-3.7,-3.65,-3.6,-3.55,-3.5,-3.45,-3.4,-3.35,-3.3,-3.25,-3.2,-3.15,-3.1,-3.05,-3,-2.95,-2.9,-2.85,-2.8,-2.75,-2.7,-2.65,-2.6,-2.55,-2.5,-2.45,-2.4,-2.35,-2.3,-2.25,-2.2,-2.15,-2.1,-2.05,-2,-1.95,-1.9,-1.85,-1.8,-1.75,-1.7,-1.65,-1.6,-1.55,-1.5,-1.45,-1.4,-1.35,-1.3,-1.25,-1.2,-1.15,-1.1,-1.05,-1,-0.95,-0.9,-0.85,-0.8,-0.75,-0.7,-0.65,-0.6,-0.55,-0.5,-0.45,-0.4,-0.35,-0.3,-0.25,-0.2,-0.15,-0.1,-0.05,0,0.05,0.1,0.15,0.2,0.25,0.3,0.35,0.4,0.45,0.5,0.55,0.6,0.65,0.7,0.75,0.8,0.85,0.9,0.95,1,1.05,1.1,1.15,1.2,1.25,1.3,1.35,1.4,1.45,1.5,1.55,1.6,1.65,1.7,1.75,1.8,1.85,1.9,1.95,2,2.05,2.1,2.15,2.2,2.25,2.3,2.35,2.4,2.45,2.5,2.55,2.6,2.65,2.7,2.75,2.8,2.85,2.9,2.95,3,3.05,3.1,3.15,3.2,3.25,3.3,3.35,3.4,3.45,3.5,3.55,3.6,3.65,3.7,3.75,3.8,3.85,3.9,3.95,4,4.05,4.1,4.15,4.2,4.25,4.3,4.35,4.4,4.45,4.5,4.55,4.6,4.65,4.7,4.75,4.8,4.85,4.9,4.95,5,5.05,5.1,5.15,5.2,5.25,5.3,5.35,5.4,5.45,5.5,5.55,5.6,5.65,5.7,5.75,5.8,5.85,5.9,5.95,6,6.05,6.1,6.15,6.2,6.25,6.3,6.35,6.4,6.45,6.5,6.55,6.6,6.65,6.7,6.75,6.8,6.85,6.9,6.95,7,7.05,7.1,7.15,7.2,7.25,7.3,7.35,7.4,7.45,7.5,7.55,7.6,7.65,7.7,7.75,7.8,7.85,7.9,7.95,8,8.05,8.1,8.15,8.2,8.25,8.3,8.35,8.4,8.45,8.5,8.55,8.6,8.65,8.7,8.75,8.8,8.85,8.9,8.95,9,9.05,9.1,9.15,9.2,9.25,9.3,9.35,9.4,9.45,9.5,9.55,9.6,9.65,9.7,9.75,9.8,9.85,9.9,9.95,10,10.05,10.1,10.15,10.2,10.25,10.3,10.35,10.4,10.45,10.5,10.55,10.6,10.65,10.7,10.75,10.8,10.85,10.9,10.95,11,11.05,11.1,11.15,11.2,11.25,11.3,11.35,11.4,11.45,11.5,11.55,11.6,11.65,11.7,11.75,11.8,11.85,11.9,11.95,12,12.05,12.1,12.15,12.2,12.25,12.3,12.35,12.4,12.45,12.5,12.55,12.6,12.65,12.7,12.75,12.8,12.85,12.9,12.95,13,13.05,13.1,13.15,13.2,13.25,13.3,13.35,13.4,13.45,13.5,13.55,13.6,13.65,13.7,13.75,13.8,13.85,13.9,13.95,14,14.05,14.1,14.15,14.2,14.25,14.3,14.35,14.4,14.45,14.5,14.55,14.6,14.65,14.7,14.75,14.8,14.85,14.9,14.95,15,15.05,15.1,15.15,15.2,15.25,15.3,15.35,15.4,15.45,15.5,15.55,15.6,15.65,15.7,15.75,15.8,15.85,15.9,15.95,16,16.05,16.1,16.15,16.2,16.25,16.3,16.35,16.4,16.45,16.5,16.55,16.6,16.65,16.7,16.75,16.8,16.85,16.9,16.95,17,17.05,17.1,17.15,17.2,17.25,17.3,17.35,17.4,17.45,17.5,17.55,17.6,17.65,17.7,17.75,17.8,17.85,17.9,17.95,18,18.05,18.1,18.15,18.2,18.25,18.3,18.35,18.4,18.45,18.5,18.55,18.6,18.65,18.7,18.75,18.8,18.85,18.9,18.95,19,19.05,19.1,19.15,19.2,19.25,19.3,19.35,19.4,19.45,19.5,19.55,19.6,19.65,19.7,19.75,19.8,19.85,19.9,19.95,20,20.05,20.1,20.15,20.2,20.25,20.3,20.35,20.4,20.45,20.5,20.55,20.6,20.65,20.7,20.75,20.8,20.85,20.9,20.95,21,21.05,21.1,21.15,21.2,21.25,21.3,21.35,21.4,21.45,21.5,21.55,21.6,21.65,21.7,21.75,21.8,21.85,21.9,21.95,22,22.05,22.1,22.15,22.2,22.25,22.3,22.35,22.4,22.45,22.5,22.55,22.6,22.65,22.7,22.75,22.8,22.85,22.9,22.95,23,23.05,23.1,23.15,23.2,23.25,23.3,23.35,23.4,23.45,23.5,23.55,23.6,23.65,23.7,23.75,23.8,23.85,23.9,23.95,24,24.05,24.1,24.15,24.2,24.25,24.3,24.35,24.4,24.45,24.5,24.55,24.6,24.65,24.7,24.75,24.8,24.85,24.9,24.95,25,25.05,25.1,25.15,25.2,25.25,25.3,25.35,25.4,25.45,25.5,25.55,25.6,25.65,25.7,25.75,25.8,25.85,25.9,25.95,26,26.05,26.1,26.15,26.2,26.25,26.3,26.35,26.4,26.45,26.5,26.55,26.6,26.65,26.7,26.75,26.8,26.85,26.9,26.95,27,27.05,27.1,27.15,27.2,27.25,27.3,27.35,27.4,27.45,27.5,27.55,27.6,27.65,27.7,27.75,27.8,27.85,27.9,27.95,28,28.05,28.1,28.15,28.2,28.25,28.3,28.35,28.4,28.45,28.5,28.55,28.6,28.65,28.7,28.75,28.8,28.85,28.9,28.95,29,29.05,29.1,29.15,29.2,29.25,29.3,29.35,29.4,29.45,29.5,29.55,29.6,29.65,29.7,29.75,29.8,29.85,29.9,29.95,30,30.05,30.1,30.15,30.2,30.25,30.3,30.35,30.4,30.45,30.5,30.55,30.6,30.65,30.7,30.75,30.8,30.85,30.9,30.95,31,31.05,31.1,31.15,31.2,31.25,31.3,31.35,31.4,31.45,31.5,31.55,31.6,31.65,31.7,31.75,31.8,31.85,31.9,31.95,32,32.05,32.1,32.15,32.2,32.25,32.3,32.35,32.4,32.45,32.5,32.55,32.6,32.65,32.7,32.75,32.8,32.85,32.9,32.95,33,33.05,33.1,33.15,33.2,33.25,33.3,33.35,33.4,33.45,33.5,33.55,33.6,33.65,33.7,33.75,33.8,33.85,33.9,33.95,34,34.05,34.1,34.15,34.2,34.25,34.3,34.35,34.4,34.45,34.5,34.55,34.6,34.65,34.7,34.75,34.8,34.85,34.9,34.95,35,35.05,35.1,35.15,35.2,35.25,35.3,35.35,35.4,35.45,35.5,35.55,35.6,35.65,35.7,35.75,35.8,35.85,35.9,35.95,36,36.05,36.1,36.15,36.2,36.25,36.3,36.35,36.4,36.45,36.5,36.55,36.6,36.65,36.7,36.75,36.8,36.85,36.9,36.95,37,37.05,37.1,37.15,37.2,37.25,37.3,37.35,37.4,37.45,37.5,37.55,37.6,37.65,37.7,37.75,37.8,37.85,37.9,37.95,38,38.05,38.1,38.15,38.2,38.25,38.3,38.35,38.4,38.45,38.5,38.55,38.6,38.65,38.7,38.75,38.8,38.85,38.9,38.95,39,39.05,39.1,39.15,39.2,39.25,39.3,39.35,39.4,39.45,39.5,39.55,39.6,39.65,39.7,39.75,39.8,39.85,39.9,39.95,40,40.05,40.1,40.15,40.2,40.25,40.3,40.35,40.4,40.45,40.5,40.55,40.6,40.65,40.7,40.75,40.8,40.85,40.9,40.95,41,41.05,41.1,41.15,41.2,41.25,41.3,41.35,41.4,41.45,41.5,41.55,41.6,41.65,41.7,41.75,41.8,41.85,41.9,41.95,42,42.05,42.1,42.15,42.2,42.25,42.3,42.35,42.4,42.45,42.5,42.55,42.6,42.65,42.7,42.75,42.8,42.85,42.9,42.95,43,43.05,43.1,43.15,43.2,43.25,43.3,43.35,43.4,43.45,43.5,43.55,43.6,43.65,43.7,43.75,43.8,43.85,43.9,43.95,44,44.05,44.1,44.15,44.2,44.25,44.3,44.35,44.4,44.45,44.5,44.55,44.6,44.65,44.7,44.75,44.8,44.85,44.9,44.95,45,45.05,45.1,45.15,45.2,45.25,45.3,45.35,45.4,45.45,45.5,45.55,45.6,45.65,45.7,45.75,45.8,45.85,45.9,45.95,46,46.05,46.1,46.15,46.2,46.25,46.3,46.35,46.4,46.45,46.5,46.55,46.6,46.65,46.7,46.75,46.8,46.85,46.9,46.95,47,47.05,47.1,47.15,47.2,47.25,47.3,47.35,47.4,47.45,47.5,47.55,47.6,47.65,47.7,47.75,47.8,47.85,47.9,47.95,48,48.05,48.1,48.15,48.2,48.25,48.3,48.35,48.4,48.45,48.5,48.55,48.6,48.65,48.7,48.75,48.8,48.85,48.9,48.95,49,49.05,49.1,49.15,49.2,49.25,49.3,49.35,49.4,49.45,49.5,49.55,49.6,49.65,49.7,49.75,49.8,49.85,49.9,49.95,50,50.05,50.1,50.15,50.2,50.25,50.3,50.35,50.4,50.45,50.5,50.55,50.6,50.65,50.7,50.75,50.8,50.85,50.9,50.95,51,51.05,51.1,51.15,51.2,51.25,51.3,51.35,51.4,51.45,51.5,51.55,51.6,51.65,51.7,51.75,51.8,51.85,51.9,51.95,52,52.05,52.1,52.15,52.2,52.25,52.3,52.35,52.4,52.45,52.5,52.55,52.6,52.65,52.7,52.75,52.8,52.85,52.9,52.95,53,53.05,53.1,53.15,53.2,53.25,53.3,53.35,53.4,53.45,53.5,53.55,53.6,53.65,53.7,53.75,53.8,53.85,53.9,53.95,54,54.05,54.1,54.15,54.2,54.25,54.3,54.35,54.4,54.45,54.5,54.55,54.6,54.65,54.7,54.75,54.8,54.85,54.9,54.95,55,55.05,55.1,55.15,55.2,55.25,55.3,55.35,55.4,55.45,55.5,55.55,55.6,55.65,55.7,55.75,55.8,55.85,55.9,55.95,56,56.05,56.1,56.15,56.2,56.25,56.3,56.35,56.4,56.45,56.5,56.55,56.6,56.65,56.7,56.75,56.8,56.85,56.9,56.95,57,57.05,57.1,57.15,57.2,57.25,57.3,57.35,57.4,57.45,57.5,57.55,57.6,57.65,57.7,57.75,57.8,57.85,57.9,57.95,58,58.05,58.1,58.15,58.2,58.25,58.3,58.35,58.4,58.45,58.5,58.55,58.6,58.65,58.7,58.75,58.8,58.85,58.9,58.95,59,59.05,59.1,59.15,59.2,59.25,59.3,59.35,59.4,59.45,59.5,59.55,59.6,59.65,59.7,59.75,59.8,59.85,59.9,59.95,60,60.05,60.1,60.15,60.2,60.25,60.3,60.35,60.4,60.45,60.5,60.55,60.6,60.65,60.7,60.75,60.8,60.85,60.9,60.95,61,61.05,61.1,61.15,61.2,61.25,61.3,61.35,61.4,61.45,61.5,61.55,61.6,61.65,61.7,61.75,61.8,61.85,61.9,61.95,62,62.05,62.1,62.15,62.2,62.25,62.3,62.35,62.4,62.45,62.5,62.55,62.6,62.65,62.7,62.75,62.8,62.85,62.9,62.95,63,63.05,63.1,63.15,63.2,63.25,63.3,63.35,63.4,63.45,63.5,63.55,63.6,63.65,63.7,63.75,63.8,63.85,63.9,63.95,64,64.05,64.1,64.15,64.2,64.25,64.3,64.35,64.4,64.45,64.5,64.55,64.6,64.65,64.7,64.75,64.8,64.85,64.9,64.95,65,65.05,65.1,65.15,65.2,65.25,65.3,65.35,65.4,65.45,65.5,65.55,65.6,65.65,65.7,65.75,65.8,65.85,65.9,65.95,66,66.05,66.1,66.15,66.2,66.25,66.3,66.35,66.4,66.45,66.5,66.55,66.6,66.65,66.7,66.75,66.8,66.85,66.9,66.95,67,67.05,67.1,67.15,67.2,67.25,67.3,67.35,67.4,67.45,67.5,67.55,67.6,67.65,67.7,67.75,67.8,67.85,67.9,67.95,68,68.05,68.1,68.15,68.2,68.25,68.3,68.35,68.4,68.45,68.5,68.55,68.6,68.65,68.7,68.75,68.8,68.85,68.9,68.95,69,69.05,69.1,69.15,69.2,69.25,69.3,69.35,69.4,69.45,69.5,69.55,69.6,69.65,69.7,69.75,69.8,69.85,69.9,69.95,70,70.05,70.1,70.15,70.2,70.25,70.3,70.35,70.4,70.45,70.5,70.55,70.6,70.65,70.7,70.75,70.8,70.85,70.9,70.95,71,71.05,71.1,71.15,71.2,71.25,71.3,71.35,71.4,71.45,71.5,71.55,71.6,71.65,71.7,71.75,71.8,71.85,71.9,71.95,72,72.05,72.1,72.15,72.2,72.25,72.3,72.35,72.4,72.45,72.5,72.55,72.6,72.65,72.7,72.75,72.8,72.85,72.9,72.95,73,73.05,73.1,73.15,73.2,73.25,73.3,73.35,73.4,73.45,73.5,73.55,73.6,73.65,73.7,73.75,73.8,73.85,73.9,73.95,74,74.05,74.1,74.15,74.2,74.25,74.3,74.35,74.4,74.45,74.5,74.55,74.6,74.65,74.7,74.75,74.8,74.85,74.9,74.95,75,75.05,75.1,75.15,75.2,75.25,75.3,75.35,75.4,75.45,75.5,75.55,75.6,75.65,75.7,75.75,75.8,75.85,75.9,75.95,76,76.05,76.1,76.15,76.2,76.25,76.3,76.35,76.4,76.45,76.5,76.55,76.6,76.65,76.7,76.75,76.8,76.85,76.9,76.95,77,77.05,77.1,77.15,77.2,77.25,77.3,77.35,77.4,77.45,77.5,77.55,77.6,77.65,77.7,77.75,77.8,77.85,77.9,77.95,78,78.05,78.1,78.15,78.2,78.25,78.3,78.35,78.4,78.45,78.5,78.55,78.6,78.65,78.7,78.75,78.8,78.85,78.9,78.95,79,79.05,79.1,79.15,79.2,79.25,79.3,79.35,79.4,79.45,79.5,79.55,79.6,79.65,79.7,79.75,79.8,79.85,79.9,79.95,80,80.05,80.1,80.15,80.2,80.25,80.3,80.35,80.4,80.45,80.5,80.55,80.6,80.65,80.7,80.75,80.8,80.85,80.9,80.95,81,81.05,81.1,81.15,81.2,81.25,81.3,81.35,81.4,81.45,81.5,81.55,81.6,81.65,81.7,81.75,81.8,81.85,81.9,81.95,82,82.05,82.1,82.15,82.2,82.25,82.3,82.35,82.4,82.45,82.5,82.55,82.6,82.65,82.7,82.75,82.8,82.85,82.9,82.95,83,83.05,83.1,83.15,83.2,83.25,83.3,83.35,83.4,83.45,83.5,83.55,83.6,83.65,83.7,83.75,83.8,83.85,83.9,83.95,84,84.05,84.1,84.15,84.2,84.25,84.3,84.35,84.4,84.45,84.5,84.55,84.6,84.65,84.7,84.75,84.8,84.85,84.9,84.95,85,85.05,85.1,85.15,85.2,85.25,85.3,85.35,85.4,85.45,85.5,85.55,85.6,85.65,85.7,85.75,85.8,85.85,85.9,85.95,86,86.05,86.1,86.15,86.2,86.25,86.3,86.35,86.4,86.45,86.5,86.55,86.6,86.65,86.7,86.75,86.8,86.85,86.9,86.95,87,87.05,87.1,87.15,87.2,87.25,87.3,87.35,87.4,87.45,87.5,87.55,87.6,87.65,87.7,87.75,87.8,87.85,87.9,87.95,88,88.05,88.1,88.15,88.2,88.25,88.3,88.35,88.4,88.45,88.5,88.55,88.6,88.65,88.7,88.75,88.8,88.85,88.9,88.95,89,89.05,89.1,89.15,89.2,89.25,89.3,89.35,89.4,89.45,89.5,89.55,89.6,89.65,89.7,89.75,89.8,89.85,89.9,89.95,90];
thetaColumn = 'Angle\n' thetaColumn;
afAmpColumn = 'Field\n' afAmpColumn;
dbColumn = 'db\n' dbColumn;
//Create multi-dimensional array for the CSV file
var csvFileData = [
thetaColumn,
afAmpColumn,
dbColumn
];
These arrays contain a lot of decimal values. The thetaColumn definition is just to show an example of the type of data I am dealing with. The way they are export currently looks like this:
I would instead like it to look something like this:
Angle Field db
1 5 9
2 6 10
3 7 11
4 8 12
I am wondering how I would do this in the most efficient way since these arrays contain a lot of data. Any advice would be greatly appreciated!
CodePudding user response:
I would suggest not to create the 2D array, but an object with key/array pairs, where the key is the column name.
Here is a function that will produce the CSV from such an object, and a run of it on sample data:
function toCsv(data) {
const arrays = Object.values(data);
return [Object.keys(data).join(","), ...arrays[0].map((row, i) =>
arrays.map(column => column[i]).join(",")
)].join("\n");
}
let theta = [1, 2, 3, 4];
let afAmp = [5, 6, 7, 8];
let db = [9, 10, 11, 12];
// Create object, where keys are the column names
const data = { theta, afAmp, db };
// Produce CSV
const csv = toCsv(data);
console.log(csv);