maybe the title does not say the problem exactly or rather how it is.
In short, I have a table that I generate according to some data that I obtain from the database and others that must be entered.
All the data that is shown is dynamic data, that is, the first value that is observed is taken from the database, and according to that value and others, the following rows and columns are generated.
Here is the code for the part that generates the following calculations:
function Generar_Datos_inver(datos, interes) {
var anios_presupuesto = parseFloat(document.getElementById('años_proyectos').value);
var globalizacion = interes / 100;
var anios_dep = Number(datos.anos_depreciacion); //VALOR DE BDD
var precio = Number(datos.adquisi_depre); //VALOR DE BDD
var elementos = new Array();
var aux = new Array();
var totales = new Array();
var totales_final = new Array();
var contador = 0
var contador2 = 0
var cadena = '';
for (var i = 0; i < anios_presupuesto; i += (anios_dep)) {
for (var j = 0; j < anios_dep + 1; j++) {
if (j == 0) {
elementos.push(precio)
} else {
elementos.push(0);
}
if(contador2 <= anios_presupuesto){
aux[contador2] = elementos[contador];
contador2++
if(contador == anios_presupuesto){
for (let k = 0; k < aux.length; k++) {
totales[k] = 0
}
}
if(contador2 == anios_presupuesto +1){
for (let k = 0; k < aux.length; k++) {
totales_final[k] = Number(totales[k]) + Number(aux[k])
}
}
}else{
aux = []
contador2 = 0
}
if ((contador) <= anios_presupuesto) {
cadena = cadena + `<td class="pre-d td_cant_inver"><input type="number" step="any" value="${(elementos[contador]).toFixed(2)}" readonly></input></td>`
}
contador++
}
precio = precio * (1 + globalizacion)
}
return cadena;
}
The problem is that I need to do the sum of each column, and I was trying it and it's in the same code, but I'll still put it so you can see how I'm trying:
if(contador2 <= anios_presupuesto){
aux[contador2] = elementos[contador];
contador2++
if(contador == anios_presupuesto){
for (let k = 0; k < aux.length; k++) {
totales[k] = 0
}
}
if(contador2 == anios_presupuesto +1){
for (let k = 0; k < aux.length; k++) {
totales_final[k] = Number(totales[k]) + Number(aux[k])
}
console.log(totales_final);
}
}else{
aux = []
contador2 = 0
}
So when I show by console it gives me this result:
I get 2 array, but in this case because I only have 2 examples but the thing is that they could be 3...4...5....n and what I was trying was to add them in each position and generate a single array with the sum.
the console image does not have the values that appear in the first image for the reason that I had taken a screenshot before and realized later.
One way to do this is by looping through the data to create a
array
complete that stores the data by year and column.And once the loop is finished, the data obtained is added. To do this, we go through all the positions of each
array
internal (all must have the same number of elements) in all the elements of thearray
complete.EDIT to add arrays with different number of elements
In this case we take as reference the largest possible number of elements, a variable that we use to iterate through the data, and we ask if the element has a value or exists before adding.