我想确定这两个数组中有多少项是匹配的,然后将其作为数字存储在状态中。
例如:
例如:
const [score, setScore] = React.useState(0)
const selections = ["one", "two", "three"]
const allCorrectAnswers = ["four", "two", "three"]
// this should return 2
我尝试过
function checkSelectedAnswer(selections, allCorrectAnswers) {
selections.map(eachChoice =>
eachChoice === allCorrectAnswers.map(eachAnswer => eachAnswer)
? setScore(prevScore => prevScore + 1) : 0
)
}
如果可以,请解释为什么我的代码不能正常工作。
allCorrectAnswers.filter(ans => selections.includes(ans)).length
- A1exandr Belan