Hello guys, I'm trying consistently to use a map() method in order to populate the rows of a table in Javascript. I setup the table for the HTML part in the index file and now I want to get the table populated with the data coming from an array that I previosly created. The code I write so far is this but there are few problems:
1 - the callback function get the data as undefined;
2 - I need to manage the missings, I mean for example if I have only two arrays the table should be populated just with the values of these two and nothing more ( ' ' ). Now when I got to test with 1 array I have the same array values in all the table.
I hope I was clear enough with the info in the question... the code and the arrays are below.
THIS IS THE ARRAYS I HAVE. EACH STRING SHOULD BE A COLUMN VALUE.
[
[ 'TD02', '2019-12-21', '30.71', 'GEA 2 s.r.l.', 'RENATO' ],
[ 'TD01', '2019-12-21', '30.71', 'GEA 2 s.r.l.', 'RENATO' ]
]
HERE IS THE CODE
function componiTabella(dati){
const data ={
headers: ["TipoDoc.","DataDoc.","Importo","CedentePrestatore","CessionarioCommittente"],
rows: new Array(10).fill(undefined).map(callbackFn(dati))
};
function callbackFn(dati){
for (i=0; i<10;i++){
for(j=0;j<10;j++){
if (dati[i][j]== ' ')
return [' ' , ' ' ,' ' ,' ' ,' ']
else
return [dati[i][j],dati[i][j+1],dati[i][j+2],dati[i][j+3],dati[i][j+4],dati[i][j+5]]
}
}
}
app.get("/data",(req,res)=>{
res.json({
headers:data.headers,
rows:data.rows,
lastUpdated: new Date().toISOString()
});
});
What I have tried:
I tried to wrap the callback function to return in the map() method;
Tried to return the values, but got undefined with "dati";
Tried to return one single value dati[0][0], I got just one column filled but all with the same value.
Tried the for loop in the functions but I suppose it is not the right path.