【问题标题】:Total all values of a single property from an object within an object汇总对象内对象的单个属性的所有值
【发布时间】:2016-08-14 00:27:22
【问题描述】:
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 与其他新玩家之一(例如philsam)填充到像老虎队或万神殿这样的球队之一。

【问题讨论】:

  • locTeams 的构造函数需要 5 个参数,你传递了 4 个。你也错过了this.playerThree = playerThree
  • @AleksandarĐokić - 它并不真正需要 5 个参数,它可以有 5 个参数。
  • @adeneo 是的,但他没有在方法中的任何地方设置playerThree......他应该,因为它在构造函数中。
  • 糟糕,忘了补充,我修好了。

标签: javascript arrays variables object new-operator


【解决方案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)

【讨论】:

    【解决方案2】:

    如果您需要第三个玩家,只需将其添加到其他两个玩家所在的位置即可。请注意,我正在消除重复项,因为每个玩家只能计入一次总目标。

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

    【讨论】:

      猜你喜欢
      • 2016-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-11
      • 1970-01-01
      相关资源
      最近更新 更多