Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript、JS中的拆分字符串_Javascript_Arrays_String_Reactjs_Jsx - Fatal编程技术网

javascript、JS中的拆分字符串

javascript、JS中的拆分字符串,javascript,arrays,string,reactjs,jsx,Javascript,Arrays,String,Reactjs,Jsx,所以我正在开发一个测验应用程序,当它第一次启动时,我的初始状态也是这样,我还让quizApplication.js组件存储所有的问题和答案 { question: "I am task oriented in order to achieve certain goals", answers: [ { type: "Brown,D,JP", content: "Hell Ya!" }, { type:

所以我正在开发一个测验应用程序,当它第一次启动时,我的初始状态也是这样,我还让quizApplication.js组件存储所有的问题和答案

{
   question: "I am task oriented in order to achieve certain goals",
   answers: [
      {
        type: "Brown,D,JP",
        content: "Hell Ya!"
      },
      {
        type: " ",
        content: "Nah"
      }
    ]
 },
这是我设置用户答案的函数

setUserAnswer(answer) {
if (answer.trim()) {
  const answer_array = answer.split(',');
  const updatedAnswersCount = update(this.state.answersCount, {
    [answer]: {$apply: (currentValue) => currentValue + 1},
  });
  this.setState({
    answersCount: updatedAnswersCount,
    answer: answer
  }); 
 }
}
我还有一个答案选项组件,如下所示

function AnswerOption(props) {
 return (
   <AnswerOptionLi>
    <Input
     checked={props.answerType === props.answer}
     id={props.answerType}
     value={props.answerType}
     disabled={props.answer}
     onChange={props.onAnswerSelected}
   />
    <Label className="radioCustomLabel" htmlFor={props.answerType}>
     {props.answerContent}
    </Label>
   </AnswerOptionLi>
  );
 }
功能应答选项(道具){
返回(
{props.answerContent}
);
}

所以我要做的就是每当用户点击HellYa!它将“Brown”和“D”以及“JP”增加+1,但现在它给了我一个新的answersCount值,即Brown,D,JP:null,那么我应该如何实现呢?非常感谢

您已拆分了
类型,但尚未使用它们

由于您已拆分了
类型
,您将得到长度为
3的
应答数组
,其中包含
[“Brown”、“D”、“JP”]

接下来,您将使用更新的答案计数更新您的状态。您正在执行以下操作

const updatedAnswersCount = update(this.state.answersCount, {
 [answer]: {$apply: (currentValue) => currentValue + 1},
});
这里的
答案
包含
“布朗,D,JP”
。因为您希望将每个值更新+1,所以让我们在分割值上循环并更新它

let updatedAnswersCount = null;

answer_array.forEach((key) => {
 updatedAnswersCount = update(this.state.answersCount, {
  [answer]: {$apply: (currentValue) => currentValue + 1},
 });
}

在这里,我假设你的类型是独一无二的。意思
Brown
/
D
/
JP
仅用于此答案,不用于其他任何内容。因此,我们假设所有更新都具有相同的值。

您的问题仍然不清楚更新功能的实现在哪里?此外,我还介绍了您使用AnswerOption组件的容器?此外,如果您根据文档使用react 16,则不应根据当前状态设置下一个状态,因为setState函数现在是异步的,多个setState可以在引擎盖下拼接在一起,请阅读此处:foreach有意义,但是它仍然给了我新的answerCount值,比如Brown,D,JP:null,我在构造函数中的初始状态是这样的:{Green:0,Brown:0,Blue:0,Red:0,A:0,B:0,C:0,D:0,EI:0,SN:0,TF:0,JP:0},好的,因此,对于第一部分,我没有传递密钥参数,现在我将密钥参数改为应答,当我使用console.log(updatedAnswersCount)时,它将通过Brown update 1和D update 1等提供我想要的。但是当我返回到我的状态时,应答仅将最后一个值JP更新1,我不知道你是否知道我的意思
let updatedAnswersCount = null;

answer_array.forEach((key) => {
 updatedAnswersCount = update(this.state.answersCount, {
  [answer]: {$apply: (currentValue) => currentValue + 1},
 });
}