function player(name, goals, games) {
this.name = name;
this.goals = goals;
this.games = games;
}
var ricky = new player('Ricky', 7, 15);
var tom = new player('Tom', 15, 17);
var phil = new player('Phillip', 9, 14);
var jerry = new player('Jerry', 11, 15);
var randy = new player('Randy', 4, 16);
var sam = new player('Sam', 5, 11);
function locTeams(name, town, wins, playerOne, playerTwo, playerThree) {
this.name = name;
this.town = town;
this.wins = wins;
this.playerOne = playerOne;
this.playerTwo = playerTwo;
this.playerThree = playerThree;
}
var tigers = new locTeams('Great Tigers', 'Clayton', 9, ricky, tom);
var pantheon = new locTeams('The Pantheons', 'Brookedale', 8, jerry, randy);
teams = [tigers, pantheon];
var totalGoalsState =
我需要一种简单的方法让totalGoalsState
等于teams
数组中玩家的累积玩家目标。另外,如何将playerThree
与其他新玩家(例如phil
或sam
)填充到其中一个像老虎或万神队这样的团队中。
答案 0 :(得分:1)
这是为阵容中的所有球队添加目标的一种方法,迭代并减少每个球员,每个球队等的值。
function player(name, goals, games) {
this.name = name;
this.goals = goals;
this.games = games;
}
function player(name, goals, games) {
this.name = name;
this.goals = goals;
this.games = games;
}
var ricky = new player('Ricky', 7, 15);
var tom = new player('Tom', 15, 17);
var phil = new player('Phillip', 9, 14);
var jerry = new player('Jerry', 11, 15);
var randy = new player('Randy', 4, 16);
var sam = new player('Sam', 5, 11);
function locTeams(name, town, wins, playerOne, playerTwo, playerThree) {
this.name = name;
this.town = town;
this.wins = wins;
this.playerOne = playerOne;
this.playerTwo = playerTwo;
this.playerThree = playerThree;
}
var tigers = new locTeams('Great Tigers', 'Clayton', 9, ricky, tom);
var pantheon = new locTeams('The Pantheons', 'Brookedale', 8, jerry, randy);
var teams = [tigers, pantheon];
function getGoals(team) {
return Object.keys(team).map(function(k) {
return k.indexOf('player') === 0 ?
team[k] && "goals" in team[k] ? team[k].goals : 0 : 0;
}).reduce(function(a,b) { return a+b });
}
var totalGoalsState = teams.reduce(function(a,b) {return getGoals(a) + getGoals(b)});
console.log(totalGoalsState)
答案 1 :(得分:0)
如果您需要第三个玩家,只需将其添加到其他两个玩家的位置即可。请注意,我正在消除重复,因为每个玩家只能计入一次总目标。
function player(name, goals, games) {
this.name = name;
this.goals = goals;
this.games = games;
}
var ricky = new player('Ricky', 7, 15);
var tom = new player('Tom', 15, 17);
var phil = new player('Phillip', 9, 14);
var jerry = new player('Jerry', 11, 15);
var randy = new player('Randy', 4, 16);
var sam = new player('Sam', 5, 11);
function locTeams(name, town, wins, playerOne, playerTwo, playerThree) {
this.name = name;
this.town = town;
this.wins = wins;
this.playerOne = playerOne;
this.playerTwo = playerTwo;
}
var tigers = new locTeams('Great Tigers', 'Clayton', 9, ricky, tom);
var pantheon = new locTeams('The Pantheons', 'Clayton', 9, jerry, randy);
teams = [tigers, pantheon];
var totalGoalsState = teams.reduce((p, c) => p.concat([c.playerOne, c.playerTwo]), [])
.reduce((p, c) => p.includes(c) ? p : p.concat(c), [])
.reduce((p, c) => p + c.goals, 0);
console.log(totalGoalsState);