Javascript 将变量传递给 foreach 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39144210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:15:53  来源:igfitidea点击:

pass a variable to foreach function

javascriptvariablesforeachscopeecmascript-6

提问by Christoph

Hi I want pass antwoordto

嗨,我想传递antwoord

opleidingArray.forEach(haalScoresOp, antwoord);

opleidingArray.forEach(haalScoresOp, antwoord);

So I can use it in the

所以我可以在

HaalScoresOp

HaalScoresOp

function. I cannot get this to work. I also tried binding but this does not function.

功能。我不能让它工作。我也试过绑定,但这不起作用。

I am getting antwoord is not defined as an error.

我得到 antwoord 未定义为错误。

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(haalScoresOp, antwoord);
  // score nog doorgeven aan haalscores op = het item
}

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);
}

回答by deceze

The way you're referencing antwoordinside haalScoresOpis 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:

您在antwoord内部引用的方式haalScoresOp无效/无意义/不好。你引用它就好像它是一个范围内的变量......好吧,它不是。该函数应该像其他参数一样接受它作为参数:

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));

回答by user12387426

You can simply use :

您可以简单地使用:

opleidingArray.forEach(haalScoresOp, antwoord);

And refer to antwoord inside the haalScoresOp function as follow:

并在 haalScoresOp 函数中引用 antwoord 如下:

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

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

antwoord 作为“this”对象传递给关于类型的函数

回答by kurdtpage

You could change the haalScoresOpfunction to be an anonymous function inside the berekenEindresultatenfunction:

您可以将haalScoresOp函数更改为函数内的匿名berekenEindresultaten函数:

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 antwoordvariable inside the berekenEindresultatenfunction

这将使antwoord变量的范围保持在berekenEindresultaten函数内