Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

306
Vistas
why nested foreach within javascript loop not working?

I am trying to check if the answers from a user are correct. The answers of the user are stored in variable "stad". The correct options are stored in variable "collectie". However, this variable is an array with a nested array. So i first loop through the "collectie", check if the collectie element is not an array and if not, check that the submitted value is within this collectie element.

If the collectie element is an array, i have to alter a little bit the function so the variable checks whether the answer is within the nested array.

I have the following:

function nakijken() {
var collectie = ["parijs", "8", "ijsselmeer", ["Volkswagen", "Audi", "Opel", "Porsche", "BMW", "Mercedes", "Mercedes-Benz"],
["Texel", "Vlieland", "Terschelling", "Ameland", "Schiermonnikoog"]];
var stad = [];
var a = 0;
stad.push(document.getElementsByTagName("input"));
collectie.forEach(uitpakken);
function uitpakken(antwoord) {
    if (!Array.isArray(antwoord)) {
        stad.forEach(myfunction);
        function myfunction(item) {
            if (antwoord.includes(item.value.toLowerCase())) {
                item.style.background = "green";
                a++;
            } else {
                antwoord.style.background = "red";
            }
        }
    }
    else{
        antwoord.Foreach(uitpakken);
        function uitpakken(antwoord) {
        stad.forEach(mysecondfunction);
            function mysecondfunction(item) {
                if (antwoord.includes(item.value.toLowerCase())) {
                    item.style.background = "green";
                    a++;
                } else {
                    antwoord.style.background = "red";
                }
            }
        }
    }
}

However, i get the error: item.value is not defined. Within the console, i see that item is a collection of inputs, and not a single input.

Why is this not working?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The main probelm seems to be in

stad.push(document.getElementsByTagName("input"));

because that pushes the returned element collection into stad as a single reference (and also an HTML collection is not a real array). Instead, you can use

stad = Array.from(document.getElementsByTagName("input"));

Second, in the first branch of the if statement, where antwoord is not an array, what does it mean if (antwoord.includes(item.value.toLowerCase())) ? You probably want to use a simple comparison like if (antwoord === item.value.toLowerCase()) .

Third, there is a typo Foreach instead of forEach in one place.

Fourth, are you sure you want to loop through all the input boxes for each entry in the main correct answers array? Isn't each input box corresponding to a specific question with a specific correct answer or set of correct answers?

about 4 years ago · Juan Pablo Isaza Denunciar

0

document.getElementsByTagName does not return an array, it returns a live HTML collection, which cannot be iterated over with forEach, although you can refer to its elements by index number (https://developer.mozilla.org/en-US/docs/Web/API/document/getElementsByTagName).
In addition, you are then pushing this entire collection to the stad array as one item, which is why you see item is a collection of inputs -- that collection (of live HTML elements) is the only item in the stad array.

As another answer suggests, instead of using stad.push, simply assign a real array to stad by creating an array from the collection:

stad = Array.from(document.getElementsByTagName("input"));

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda