【发布时间】:2021-05-07 04:44:52
【问题描述】:
为此,我构建了几个函数和一个高阶函数。我希望将三个较小的函数作为参数传递给高阶函数。当我尝试调用高阶函数并作为参数传递时,出现回调不是函数的错误。有什么想法吗?
{
"Year": 2002,
"Datetime": "02 Jun 2002 - 18:30",
"Stage": "Group F",
"Stadium": "Saitama Stadium 2002",
"City": "Saitama",
"Home Team Name": "England",
"Home Team Goals": 1,
"Away Team Goals": 1,
"Away Team Name": "Sweden",
"Win conditions": "",
"Attendance": 52721,
"Half-time Home Goals": 1,
"Half-time Away Goals": 0,
"Referee": "SIMON Carlos (BRA)",
"Assistant 1": "OLIVEIRA Jorge (BRA)",
"Assistant 2": "DUPANOV Yuri (BLR)",
"RoundID": 43950100,
"MatchID": 43950005,
"Home Team Initials": "ENG",
"Away Team Initials": "SWE"
},
{
"Year": 2002,
"Datetime": "02 Jun 2002 - 20:30",
"Stage": "Group B",
"Stadium": "Gwangju World Cup Stadium",
"City": "Gwangju",
"Home Team Name": "Spain",
"Home Team Goals": 3,
"Away Team Goals": 1,
"Away Team Name": "Slovenia",
"Win conditions": "",
"Attendance": 28598,
"Half-time Home Goals": 1,
"Half-time Away Goals": 0,
"Referee": "GUEZZAZ Mohammed (MAR)",
"Assistant 1": "TOMUSANGE Ali (UGA)",
"Assistant 2": "BEREUTER Egon (AUT)",
"RoundID": 43950100,
"MatchID": 43950008,
"Home Team Initials": "ESP",
"Away Team Initials": "SVN"
},
]
function getFinals(arr)
{
return arr.filter(team => team.Stage === "Final");
}
//console.log(getFinals(fifaData));
function getYears(arr, callback)
{
const years = callback(arr).map(team => team.Year);
return years;
}
function getWinners(arr, func)
{
const winners = [];
const winIt = func(arr);
console.log(winIt);
for (let i = 0; i < winIt.length; i++)
{
if(winIt[i]['Home Team Goals'] > winIt[i]['Away Team Goals'])
{
winners.push(winIt[i]['Home Team Name']);
}
else
{
winners.push(winIt[i]['Away Team Name']);
}
}
return winners
}
function getWinnersByYear(arr, cbThree, cbFour,cbFive)
{
const years = cbThree(arr, cbFive);
const winners = cbFour(arr, cbThree);
const full = [];
for(let j = 0; j < years.length; j++)
{
full.push(`In ${years[j]}, ${winners[j]} won the world cup!`);
}
return full;
}
console.log(getWinnersByYear(fifaData, getYears, getWinners, getFinals));
【问题讨论】:
标签: javascript callback higher-order-functions