I have to save the difference of X files (pages of a pdf) and I need the differences of that page to be saved in each array (one per page).
I have managed to generate empty arrays dynamically, but my problem is how to push those arrays dynamically as well?
The content that must be "dynamic", since I don't know the total number of pages can vary, is that of the nested if that I show in the code:
//genera dinámicamente los arrays en los que se guardaran diferencias de cada página (el número total de páginas es allPages.length)
for (i=1; i<=allPages.length; i++) {
eval("diffPage"+i+"="+"[]");
}
var pairs = []; //Ej. var combinations = ["texto1","texto2","4"];
for (var x=0; x<pairs.length; x++){
var diff = recognizeTerms(comb[x], 5, 50); //diferencias de todas las combinaciones posibles
for (y=1; y<allPages.length; y++) {
for (z=0; z<diff.length; z++) {
//A partir de aquí:
//¿Hay alguna manera de hacer este guardado en arrays dinámico?
//Ya que se deben guardar las diferencias de cada página en un array
if (combinations[x][2] == 1) {
if (diffPage1.includes(diff[z]) == false) { //comprueba que el string no existe ya en el array de diferencias
diffPage1.push(diff[z]);
}
}
if (combinations[x][2] == 2) {
if (diffPage2.includes(diff[z]) == false) {
diffPage2.push(diff[z]);
}
}
if (combinations[x][2] == 3) {
if (diffPage3.includes(diff[z]) == false) {
diffPage3.push(diff[z]);
}
}
if (combinations[x][2] == 4) {
if (diffPage4.includes(diff[z]) == false) {
diffPage4.push(diff[z]);
}
}
//sucesivos if ... (tantos como números de página haya), por ello necesito que sean dinámicos esos if
}
}
}
What you have to do is save the arrays in an array. That way you fix two things: you don't use
eval
, which is always a bad idea, and you know the number of arrays you have:So your first loop looks like this:
and your innermost loop looks like this:
And the loop
for (y=1; y<allPages.length; y++) {
is left over because you're not using ity
for anything.