简体   繁体   English

如何根据某些条件在数组内的对象中增加变量?

[英]How can I increment a variable within an object that is within an array based on certain conditions?

I'm brand new to coding and javascript (4 weeks) so forgive me if this is pretty basic. 我是编码和javascript的新手(4周),如果这很基础,请原谅我。

I'm working on creating a Disc Golf score tracker web application that works like this: you input the course, player names, and scores and on submit, it creates a scorecard and outputs it to the page. 我正在创建一个Disc Golf得分跟踪器Web应用程序,该应用程序的工作原理如下:您输入课程,球员名称和得分,并在提交时创建一个记分卡并将其输出到页面。 From there I want it to determine who the winner for each round is and then calculate a win/loss ratio for all available scorecards, which will then output to a leaderboard. 从那里,我希望它确定每个回合的获胜者是谁,然后计算所有可用记分卡的赢/输比率,然后将其输出到排行榜。

I'm stuck on changing the value of the win/loss variables in my array of objects. 我坚持要更改对象数组中获胜/失败变量的值。

My idea for changing this was to have it happen before it is put into local storage, when each scorecard is submitted. 我要更改此设置的想法是,在提交每个记分卡时将其放入本地存储之前进行。 I've tried to used conditional statements like below but they don't seem to work. 我试图使用如下条件语句,但它们似乎不起作用。 Should this be done in a separate function? 是否应该在单独的功能中完成? I currently have the array of objects being created, followed by these variables being created, and then the conditional statement below. 我目前正在创建对象数组,然后创建这些变量,然后是下面的条件语句。 Is this in the wrong order? 这是顺序错误吗? I've been stuck on this for a while and just can figure out what I'm doing wrong. 我已经坚持了一段时间,只能弄清楚我做错了什么。

let scoreOneInput = scorecardInput[1].score;
let winOne = scorecardInput[1].win;
let lossOne = scorecardInput[1].loss;

let scoreTwoInput = scorecardInput[2].score;
let winTwo = scorecardInput[2].win;
let lossTwo = scorecardInput[2].loss;

if (scoreOneInput <= scoreTwoInput) {
  winOne++;
};

function submitScorecard() {

  let scorecardInput = [
    {
      courseName: course
    },
    {
      name: playerOne,
      score: scoreOne,
      win: 0,
      loss: 0
    },
    {
      name: playerTwo,
      score: scoreTwo,
      win: 0,
      loss: 0
    },
    {
      name: playerThree,
      score: scoreThree,
      win: 0,
      loss: 0
    },
    {
      name: playerFour,
      score: scoreFour,
      win: 0,
      loss: 0
    },
    {
      name: playerFive,
      score: scoreFive,
      win: 0,
      loss: 0
    }
  ];

  if (localStorage.getItem('scorecard') === null) {
    let scorecard = [];
    scorecard.push(scorecardInput);
    localStorage.setItem('scorecard', JSON.stringify(scorecard));
  } else {
    let scorecard = JSON.parse(localStorage.getItem('scorecard'));
    scorecard.push(scorecardInput);
    localStorage.setItem('scorecard', JSON.stringify(scorecard));
  }
}

Numbers are passed by reference in javascript, meaning each variable its own unique instance. 数字是通过javascript中的引用传递的,这意味着每个变量都有自己的唯一实例。 Changing one, will not change another. 更改一个,不会更改另一个。

For example: 例如:

let a = 1
let b = a
a++
console.log(a) //=> 2
console.log(b) //=> 1

You need to assign your new number back into your data structure: 您需要将新编号分配回数据结构中:

if (scoreOneInput <= scoreTwoInput) {
  scorecardInput[2].win = scorecardInput[2].win + 1;
};

Or you could increment the number directly on your scorecard object: 或者,您可以直接在记分卡对象上增加数字:

if (scoreOneInput <= scoreTwoInput) {
  scorecardInput[2].win++;
};

I think you have to calculate the maximum score in the round. 我认为您必须计算一轮的最高分数。 You shouldn't just compare two scores. 您不应该只比较两个分数。 So first, you have to reorganize your scorecardInput: 因此,首先,您必须重新组织您的ScorecardInput:

let scorecardInput = [
courseName: course
players: [
   {
     name: playerOne,
     score: scoreOne,
     win: 0,
     loss: 0
   },
   {
     name: playerTwo,
     score: scoreTwo,
     win: 0,
     loss: 0
   },
   ...
]
];

Then you calculate the maximum score and increment the number of wins for that player: 然后,您可以计算最高分数并增加该玩家的获胜次数:

   scorecardInput.players = scorecardInput.players.map(function(player) {
      if(player.score == Math.max.apply(Math,scorecardInput.players.map(function(p) { 
                    return p.score;  
                })))
             player.win++;
             return player;
      })
    }
  );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM