I have a JSON with internal arrays:
JavaScript
x
12
12
1
{
2
"configurable": true,
3
"esquema": {
4
"no_abono": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
5
"id_unico": ["60a41a721e028", "60a41a721e04f", "60a41a721e05f", "60a41a721e06b", "60a41a721e076", "60a41a721e081", "60a41a721e08c", "60a41a721e098", "60a41a721e0a4", "60a41a721e0b0", "60a41a721e0bb", "60a41a721e0c6", "60a41a721e0d2", "60a41a721e0dd", "60a41a721e0e8"],
6
"dia_semana": ["Monday", "Wednesday", "Friday", "Sunday", "Tuesday", "Thursday", "Saturday", "Monday", "Wednesday", "Friday", "Sunday", "Tuesday", "Thursday", "Saturday", "Monday"],
7
"fecha": ["2021-05-17", "2021-05-19", "2021-05-21", "2021-05-23", "2021-05-25", "2021-05-27", "2021-05-29", "2021-05-31", "2021-06-02", "2021-06-04", "2021-06-06", "2021-06-08", "2021-06-10", "2021-06-12", "2021-06-14"],
8
"abono": [80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80],
9
"remanente": [1040, 960, 880, 800, 720, 640, 560, 480, 400, 320, 240, 160, 80, 0, -80]
10
}
11
}
12
My code to write the table is:
JavaScript
1
14
14
1
var tabla = '';
2
for (i in resultado.esquema) {
3
tabla += '<tr>';
4
tabla += '<td>' + resultado.esquema["no_abono"][i] + '</td>';
5
tabla += '<td>' + resultado.esquema["id_unico"][i] + '</td>';
6
tabla += '<td>' + resultado.esquema["fecha"][i] + '</td>';
7
tabla += '<td>' + resultado.esquema["dia_semana"][i] + '</td>';
8
tabla += '<td>' + resultado.esquema["remanente"][i] + '</td>';
9
tabla += '<td>' + resultado.esquema["abono"][i] + '</td>';
10
tabla += '</tr>';
11
}
12
$('#tbl_abonos_prestamo').append(tabla);
13
14
But always run as undefined
How to get internals values in array?
Advertisement
Answer
You could make things easier by pre-processing the data and making an array of objects with all properties (rows). Then, creating your table will become simpler:
JavaScript
1
20
20
1
var resultado = {"configurable":true,"esquema":{"no_abono":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],"id_unico":["60a41a721e028","60a41a721e04f","60a41a721e05f","60a41a721e06b","60a41a721e076","60a41a721e081","60a41a721e08c","60a41a721e098","60a41a721e0a4","60a41a721e0b0","60a41a721e0bb","60a41a721e0c6","60a41a721e0d2","60a41a721e0dd","60a41a721e0e8"],"dia_semana":["Monday","Wednesday","Friday","Sunday","Tuesday","Thursday","Saturday","Monday","Wednesday","Friday","Sunday","Tuesday","Thursday","Saturday","Monday"],"fecha":["2021-05-17","2021-05-19","2021-05-21","2021-05-23","2021-05-25","2021-05-27","2021-05-29","2021-05-31","2021-06-02","2021-06-04","2021-06-06","2021-06-08","2021-06-10","2021-06-12","2021-06-14"],"abono":[80,80,80,80,80,80,80,80,80,80,80,80,80,80,80],"remanente":[1040,960,880,800,720,640,560,480,400,320,240,160,80,0,-80]}};
2
3
var rows = resultado.esquema.no_abono.map(
4
(_, i) => Object.fromEntries(
5
Object.entries(resultado.esquema).map(([k, v]) => [k, v[i]])
6
)
7
);
8
9
var tabla = rows.map(row => `
10
<tr>
11
<td>${row.no_abono}</td>
12
<td>${row.id_unico}</td>
13
<td>${row.fecha}</td>
14
<td>${row.dia_semana}</td>
15
<td>${row.remanente}</td>
16
<td>${row.abono}</td>
17
</tr>
18
`).join('');
19
20
console.log(tabla);