javascript 在javascript中拆分字符串,reactjs
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46570887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
split string in javascript, reactjs
提问by Irfan
so I'm developing a quiz application sort of, so thisis my initial state of the app when it first launch, I also have quizApplication.js component store all the question and answers,
所以我正在开发一个测验应用程序,所以这是我第一次启动时应用程序的初始状态,我还有 quizApplication.js 组件存储所有问题和答案,
{
question: "I am task oriented in order to achieve certain goals",
answers: [
{
type: "Brown,D,JP",
content: "Hell Ya!"
},
{
type: " ",
content: "Nah"
}
]
},
and here is my function to set the user answer
这是我设置用户答案的功能
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
});
}
}
I also have a AnswerOption component like so
我也有一个像这样的 AnswerOption 组件
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>
);
}
So what im try to do is that whenever the user click on HellYa! it will increment "Brown" and "D" and "JP" by +1, but right now it gives me a new answersCount value as Brown,D,JP: null, so how should I achieve this? Many thanks!
所以我试图做的是,每当用户点击 HellYa!它会将“Brown”和“D”和“JP”增加+1,但现在它给了我一个新的 answersCount 值作为 Brown,D,JP: null,那么我应该如何实现呢?非常感谢!
回答by Panther
You have split your type, but havent made use of them yet.
你已经拆分了你的type,但还没有使用它们。
As you have split your type, you would get answer_arraywith a length of 3containing ["Brown", "D", "JP"]
当你拆分你的type,你会得到answer_array一个3包含的长度["Brown", "D", "JP"]
const answer_array = answer.split(',');
Next you are updating your state with the updated answer count. You are performing the below
接下来,您将使用更新的答案计数更新您的状态。您正在执行以下操作
const updatedAnswersCount = update(this.state.answersCount, {
[answer]: {$apply: (currentValue) => currentValue + 1},
});
Here answercontains "Brown,D,JP". Since you want to update each of it by +1, lets loop over the split value and update it.
这里answer包含"Brown,D,JP". 由于您想通过 +1 更新每个,让我们遍历拆分值并更新它。
let updatedAnswersCount = null;
answer_array.forEach((key) => {
updatedAnswersCount = update(this.state.answersCount, {
[answer]: {$apply: (currentValue) => currentValue + 1},
});
}
Here, i'm assuming that your type is unique. Meaning Brown/D/JPis present only for this answer and not for anything else. So we are assuming all will have same value.
在这里,我假设您的类型是独一无二的。含义Brown/ D/JP只是这个答案,不为别的礼物。所以我们假设所有的值都相同。

