简体   繁体   English

更改状态输入,它是一个存在于 JSON 数组中的 JSON 对象

[英]Change the state input which is a JSON Object that is present inside an array of JSON

初始输出 For now my code looks like this.现在我的代码看起来像这样。 it is basically an an quiz app.它基本上是一个测验应用程序。 By default when the page loads the question appear at bottom.默认情况下,当页面加载时,问题出现在底部。 we have four contestants and before answering a question one have to click on any of the buzzer button given after that when clicking on the options if correct it will throw an alert saying correct answer and will increase the point of that particular contestant by 3 if wrong answer then decrease the value of point by 1.我们有四名参赛者,在回答问题之前,必须点击之后给出的任何蜂鸣器按钮,如果点击选项正确,它会发出警报,说正确答案,如果错误,该特定参赛者的分数将增加 3 answer 然后将 point 的值减 1。

After clicking on the buzzer button the state has an object called playerBuzzer which is by default null get changed to specified index no.单击蜂鸣器按钮后,状态有一个名为 playerBuzzer 的对象,默认情况下为 null 更改为指定的索引号。 of array for eg if james buzzer is clicked playerBuzzer changes to 0,if steve buzzer is clicked playerBuzzer changes to 3. so now what i want is that , so suppose i click the buzzer button now the state of playerBuzzer changes to a particular index now i want to change the "point" of that particular index of array increment if correct answer decrement if wrong answer(i will be showing only the necessary code )例如,如果单击 james buzzer,playerBuzzer 更改为 0,如果单击 steve buzzer playerBuzzer 更改为 3。所以现在我想要的是,所以假设我现在单击蜂鸣器按钮 playerBuzzer 的状态现在更改为特定索引如果正确答案减少如果错误答案,我想更改该特定数组增量索引的“点”(我将仅显示必要的代码)

class Sec2 extends Component {
  state = {
    players: [
      {
        name: "James",
        point: 0
      },
      {
        name: "Julia",
        point: 0
      },
      {
        name: "Martha",
        point: 0
      },
      {
        name: "Steve",
        point: 0
      }
    ],
buzz(z) {//changes the playerBuzzer when buzzer button is clicked
    if (z == "James") {
      this.setState({ playerBuzzer: 0 });
    }
    if (z == "Julia") {
      this.setState({ playerBuzzer: 1 });
    }
    if (z == "Martha") {
      this.setState({ playerBuzzer: 2 });
    }
    if (z == "Steve") {
      this.setState({ playerBuzzer: 3 });
    }
  }
 checkAnswer(x) { //When the option under question is clicked this method 
                   // happens
    if (this.state.currentQuestion == 1 && x == "New Delhi") {
      alert("Correct Answer");
      this.setState({ currentQuestion: 2 });
    } else if (this.state.currentQuestion == 1 && x != "New Delhi") {
      alert("Wrong Answer");
      this.setState({ currentQuestion: 2 });
    }
     if (this.state.currentQuestion == 2 && x == "Paris") {
      alert("Correct Answer");
      this.setState({ currentQuestion: 3 });
    } else if (this.state.currentQuestion == 2 && x != "Paris") {
      alert("Wrong Answer");
      this.setState({ currentQuestion: 3 });
    }
    if (this.state.currentQuestion == 3 && x == "Pound") {
      alert("Correct Answer");
      this.setState({ currentQuestion: 4 });
    } else if (this.state.currentQuestion == 3 && x != "Pound") {
      alert("Wrong Answer");
      this.setState({ currentQuestion: 4 });
    }
     if (this.state.currentQuestion == 4 && x == "8848 m") {
      alert("Correct Answer");
      this.setState({ currentQuestion: 5 });
    } else if (this.state.currentQuestion == 4 && x != "8848 m") {
      alert("Wrong Answer");
      this.setState({ currentQuestion: 5 });
    }
    if  (this.state.currentQuestion == 5 && x == "Tokyo") {
      alert("Correct Answer");
      this.setState({ currentQuestion: 6 });
    }     else if (this.state.currentQuestion == 5 && x != "Tokyo") {
      alert("Wrong Answer");
      this.setState({ currentQuestion: 6 });
    }
  }[![initial output][1]][1]

I doesn't have all the context, so maybe I miss something ;)我没有所有的上下文,所以也许我错过了一些东西;)

I rewrite your state management to make things clearer.我重写了您的状态管理以使事情更清楚。

But I don't know from where you fetch your questions and answers, so I hope it helps you at least.但我不知道你从哪里得到你的问题和答案,所以我希望它至少对你有帮助。

... your class
  state = {
    playerBuzzer: null,
    players: {
      James: {
        name: "James",
        point: 0
      },
      Julia: {
        name: "Julia",
        point: 0
      },
      Martha: {
        name: "Martha",
        point: 0
      },
      Steve: {
        name: "Steve",
        point: 0
      }
    },
    currentQuestion: 1,
    answers: {
      1: "New Delhi",
      2: "Paris" // and so on
    }
  };

  buzz(playerId) {
    //changes the playerBuzzer when buzzer button is clicked
    this.setState({
      playerBuzzer: playerId
    });
  }

  checkAnswer(answer) {
    const { answers, currentQuestion, playerBuzzer, players } = this.state; // This is know as destructuration. You "explode" the state in variables.
    const { point } = players[playerBuzzer];
    const isCorrect = answers[currentQuestion] === answer; // We get the response for the current question. Normalization makes things magic :)
    const nextQuestion = currentQuestion + 1;
    //When the option under question is clicked this method
    // happens
    let updatedPoints;

    if (isCorrect) {
      updatedPoints = point + 1;
      alert("Correct Answer");
    } else {
      updatedPoints = point - 1;
      alert("Wrong Answer");
    }

    this.setState({
      players: {
        ...players, // required to not delete all users :)
        [playerBuzzer]: {
          point: updatedPoints
        }
      },
      currentQuestion: nextQuestion
    });
  }

The idea is to "normalize" your data structure to make things easier.这个想法是“规范化”你的数据结构,让事情变得更容易。

This is not directly related to your question, but you can learn a lot with this link (redux is a superior state management for react apps but out of the scope here ;))这与您的问题没有直接关系,但是您可以通过此链接学到很多东西(redux 是一种出色的 React 应用程序状态管理,但超出了此处的范围;))

https://redux.js.org/recipes/structuring-reducers/normalizing-state-shape https://redux.js.org/recipes/structuring-reducers/normalizing-state-shape

Each player should be it's own class.每个玩家都应该是自己的班级。

class Player{
   constructor(name)..
}

To each button you assign that player with event listener.为每个按钮分配具有事件侦听器的播放器。 Then you run your checkAnswer function (which should return boolean).然后你运行你的 checkAnswer 函数(它应该返回布尔值)。 If true player.score += 3 else player.score--;如果为 true player.score += 3 else player.score--;

And I think that you should assign to each answer only boolean values like with class="bad-answer" / "good-answer".而且我认为您应该只为每个答案分配布尔值,例如 class="bad-answer" / "good-answer"。 Because you won't have headaches when generating dynamically content.因为在生成动态内容时您不会头疼。 :) :)

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

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