Hi I want pass the var antwoord
to
opleidingArray.forEach(haalScoresOp, antwoord);
So I can use it in the function HaalScoresOp
var antwoordenPerVraag = [2,1,3];
function haalScoresOp(item, index) {
console.log("haal score op voor");
console.log(item.naam, item.scores);
console.log("haal antwoord op", antwoord);
}
function berekenEindresultaten(item, index)
{
var opleidingArray = VragenEnScores.vragen[index].opleidingen;
var antwoord = "bla";
opleidingArray.forEach(haalScoresOp, antwoord);
}
antwoordenPerVraag.forEach(berekenEindresultaten);
Just use the array outside of data.
The forEach method does not return a new array like other iterators such as filter , map and sort . Instead, the method returns undefined itself. So it's not chainable like those other methods are.
From the official MDN docs: There is no way to stop or break a forEach() loop other than by throwing an exception. If you need such behavior, the forEach() method is the wrong tool.
The way you're referencing antwoord
inside haalScoresOp
is invalid/nonsense/not good. You're referencing it as if it was a variable in scope… well, it's not. The function should accept it as parameter just like its other parameters:
function haalScoresOp(antwoord, item, index) {
..
console.log(antwoord);
}
Then you can pass it in on the caller's side:
opleidingArray.forEach(function (item, index) {
haalScoresOp(antwoord, item, index)
});
or:
opleidingArray.forEach(haalScoresOp.bind(null, antwoord));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With