26

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);
  • I tried binding but this does not function.
  • I am getting antwoord is not defined as an error.
5
  • Where you're calling opleidingArray.forEach(haalScoresOp, antwoord);, you're passing in the antwoord variable as a parameter - this is just a string, hence your "antwoord is not defined as an error" message. Try removing the antwoord parameter from the forEach call Commented Aug 25, 2016 at 11:38
  • 1
    antwoord in haalScoresOp() is not in scope, its local to berekenEindresultaten()
    – Alex K.
    Commented Aug 25, 2016 at 11:38
  • @AlexK. how do I pass it to that scope?
    – Christoph
    Commented Aug 25, 2016 at 11:41
  • 3
    "I also tried binding" - That should work. var params={antwoord:"bla"}; and then .forEach(haalScoresOp.bind(params)), and then this.antwoord within the function.
    – nnnnnn
    Commented Aug 25, 2016 at 11:43
  • @nnnnnn this seems to work!
    – Christoph
    Commented Aug 25, 2016 at 11:47

5 Answers 5

33

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));
4
  • The way you are referencing haalScoresOp(antwoord, item, index) inside the forEach() is invalid/nonsense/not good. You are referencing it as if it was a variable in scope, but haalScoresOp and antwoord, are not defined inside forEach()
    – Jiren
    Commented Mar 26, 2020 at 21:44
  • 4
    They are defined in the surrounding scope though, so they are in scope.
    – deceze
    Commented Mar 27, 2020 at 7:00
  • While this is the correct answer, if you just need to access a variable inside of a forEach call you can assign the variable to the window to act as a global. Eg. window.google = google and then inside of the forEach you can use window.google
    – Grant
    Commented Jan 24, 2022 at 2:58
  • 2
    @Grant Please don’t though.
    – deceze
    Commented Jan 24, 2022 at 5:57
4

You can simply use :

opleidingArray.forEach(haalScoresOp, antwoord);

And refer to antwoord inside the haalScoresOp function as follow:

function haalScoresOp(){
    var diantwoord = this.valueOf();
}

antwoord is passed as 'this' object to the function regarding the type

4

You can use the 2nd thisArg parameter of forEach to pass an object with as many parameters you like:

const antwoordenPerVraag = [2,1,3];

// Dummy `VragenEnScores` variable to test the code: 
const VragenEnScores = {
  vragen: [
    { opleidingen: [{ naam: "A", scores: 1 }] },
    { opleidingen: [{ naam: "B", scores: 2 }] },
    { opleidingen: [{ naam: "C", scores: 3 }] }
  ]
}

antwoordenPerVraag.forEach(berekenEindresultaten);

function berekenEindresultaten(item, index) {

  const opleidingArray = VragenEnScores.vragen[index].opleidingen;
  const antwoord = "bla";
  // We are going to pass an object as the 2nd argument to forEach
  // which contains custom properties and values 
  opleidingArray.forEach(haalScoresOp, { antwoord: antwoord, extra: "Yey!" });

}

function haalScoresOp(item, index) {
  console.log("haal score op voor");
  console.log(item.naam);
  console.log(item.scores);
  console.log("haal antwoord op");
  console.log("antwoord: ", this.antwoord); //=> "bla"
  console.log("extra: ", this.extra);       //=> "Yey!"
}

A simpler, less-bloated example to understand the concept easier:

const list = [ 11, 22, 33 ];

list.forEach( 

  // 1st Argument to forEach: callback
  function( value ){

    console.log( value );
    console.log( this.extraParameter );
  
  },
  
  // 2nd Argument to forEach: thisArg
  { extraParameter: "extra" }

)
0
0

You could change the haalScoresOp function to be an anonymous function inside the berekenEindresultaten function:

var antwoordenPerVraag = [2,1,3];

console.log(VragenEnScores.vragen[0].opleidingen[0]);

antwoordenPerVraag.forEach(berekenEindresultaten);

function berekenEindresultaten(item, index) {
  var opleidingArray = VragenEnScores.vragen[index].opleidingen;

  var antwoord = "bla";

  opleidingArray.forEach(function(item, index){
    // score nog doorgeven aan haalscores op = het item
    console.log("haal score op voor");
    console.log(item.naam);
    console.log(item.scores);

    console.log("haal antwoord op");
    console.log(antwoord);
  });

}

This would keep the scope of the antwoord variable inside the berekenEindresultaten function

0

@deceze answered it, but struggled to follow how to apply to my code (obviously in too much of a rush). So I'll re-rig his answer slightly to show it:

function berekenEindresultaten(item, index) 
{
  var opleidingArray = VragenEnScores.vragen[index].opleidingen;
  var antwoord = "bla";
  opleidingArray.forEach(function (item, index) {
    haalScoresOp(antwoord, item, index)
  });
}

function haalScoresOp(MyAntwoordPassedIn, item, index) {
  ..
  console.log(MyAntwoordPassedIn);
}

For those interested in why (and didn't read the comments properly) - the important aspect was the function being declared inside the .forEach(). At that point the variable antwoord is still in-scope and accessible - it does not need to be passed in as a variable.

Not the answer you're looking for? Browse other questions tagged or ask your own question.