未定义forEach javascript函数

未定义forEach javascript函数,javascript,foreach,Javascript,Foreach,我试图实现forEach javascript函数来迭代players数组并合计一些值。当我调用函数时,我得到了错误 players.forEach(function(players){ ^ TypeError: undefined is not a function 代码如下: /*calculate the total number of points per player, use to calculate total score*/ function playerTotal(p

我试图实现forEach javascript函数来迭代
players
数组并合计一些值。当我调用函数时,我得到了错误

players.forEach(function(players){
    ^
TypeError: undefined is not a function
代码如下:

/*calculate the total number of points per player, use to calculate total score*/
function playerTotal(playerObj){
  var pacersTotal = 0;
  var hawksTotal = 0;
  var total = 0;
  total += 3*(playerObj.three_pointers_made);
  total += 2*(playerObj.field_goals_made - playerObj.three_pointers_made);
  total += playerObj.free_throws_made;
  if (playerObj.team_name == "Pacers"){
    pacersTotal += total;
  }
  else {
    hawksTotal += total;
  }
  console.log("Pacers " + pacersTotal + "\nHawks" + hawksTotal);
}

players.forEach(playerTotal(players));

换句话说,我尝试使用forEach对
players
数组中的每个值(对象)调用
playerTotal
函数,但没有用!有什么提示吗?

假设您在调用
forEach
方法之前已经定义了
players
问题就在这一行

players.forEach(playerTotal(players));
应传递功能参考而不是功能响应

成功

players.forEach(function(playerObj){

   playerTotal(playerObj);

});

您从未定义过数组
players
,是吗?
players
从何而来?您在上面显示了对
forEach
的两个调用,其中一个(尽管只显示了部分)看起来正确(在顶部,尽管参数名称已关闭),另一个不正确(在底部)。你用的是哪一个?假设他们的代码中有,而不是问题顶部的代码。但它符合错误信息,而不是问题中显示错误信息的代码。@T.J.Crowder true,
players.forEach(函数(玩家){
不同于
players.forEach(玩家总数))
。我假设是后者,因为这看起来更像是在查看错误。是的,它符合提供的错误消息,而
players
未定义,或
players。forEach
未定义,不符合引用的错误消息。我定义了数组播放器。(它非常大,所以我没有包含这部分代码。)该错误最初是针对行
players.forEach(playerTotal(players));
引发的,正如@gurvinder372所建议的,我将其更新为
players.forEach(function(playerObj){playerTotal(playerObj);
,但仍然得到相同的错误。