Javascript 如何检查此.state中的两个数组是否包含相同的值?

Javascript 如何检查此.state中的两个数组是否包含相同的值?,javascript,reactjs,Javascript,Reactjs,我有两个数组,一个包含正确答案的索引号correctAnswers 和userAnswers包含用户选择的答案的索引编号 我如何检查它们在索引中是否具有相同的值,如果是,则将+1添加到总计中 this.state = { correctAnswers: [], // [1, 2, 3, 1, 2, 1, 3, 3, 4, 3] userAnswers: [], // [1, 3, 1, 2, 2, 1, 1, 3, 4, 3] Tot

我有两个数组,一个包含正确答案的索引号
correctAnswers

userAnswers
包含用户选择的答案的索引编号

我如何检查它们在索引中是否具有相同的值,如果是,则将+1添加到
总计中

    this.state = {
        correctAnswers: [], // [1, 2, 3, 1, 2, 1, 3, 3, 4, 3]
        userAnswers: [],    // [1, 3, 1, 2, 2, 1, 1, 3, 4, 3]
        Total: 0
    };
因此,在上述情况下,总计应为
Total=6

我的尝试:

const correctAnswers = this.state.correctAnswers;
    const userAnswers = this.state.userAnswers;

    compareAnswers() {
    for (var i = 0; i < correctAnswers.length; i++) {
        if (correctAnswers[i] === userAnswers[i]) {
            this.setState({ Total: +1 });
        } else console.log("ITS NOT A RIGHT ANSWER ");
    }
   }
const correctAnswers=this.state.correctAnswers;
const userAnswers=this.state.userAnswers;
比较答案(){
对于(变量i=0;i
好的,因为它是一个对象内部的数组,而实际对象有一个
这个
引用,所以我们必须适应它

  for(var i=0;i<this.state.correctAnswers.length;i++){
  if(this.state.correctAnswers[i] === this.state.userAnswers[i]){
    this.state.Total++;
  }
}

for(var i=0;i好的,因为它是一个对象内部的数组,而实际对象有一个
这个
引用,我们必须适应它

  for(var i=0;i<this.state.correctAnswers.length;i++){
  if(this.state.correctAnswers[i] === this.state.userAnswers[i]){
    this.state.Total++;
  }
}

for(var i=0;i您的尝试看起来不错,只是有一个语法错误(您不能这样做
Total:+1
)。相反,您可以使用functional
setState
语法,对于您的代码段,该语法如下所示:

compareAnswers() {
    for (var i = 0; i < correctAnswers.length; i++) {
        if (correctAnswers[i] === userAnswers[i]) {
            this.setState(oldState => ({...oldState, Total: oldState.Total++}));
        } else console.log("ITS NOT A RIGHT ANSWER ");
    }
}
compareAnswers() {
    let newTotal = 0;
    for (var i = 0; i < correctAnswers.length; i++) {
        if (correctAnswers[i] === userAnswers[i]) {
            newTotal++;
        } else console.log("ITS NOT A RIGHT ANSWER ");
    }

    this.setState({Total: newTotal})
}
compareAnswers(){
对于(变量i=0;i({…oldState,Total:oldState.Total++});
}else console.log(“这不是一个正确的答案”);
}
}
但是,最好是对所有总和进行批处理,并在完成后只设置一次状态,如下所示:

compareAnswers() {
    for (var i = 0; i < correctAnswers.length; i++) {
        if (correctAnswers[i] === userAnswers[i]) {
            this.setState(oldState => ({...oldState, Total: oldState.Total++}));
        } else console.log("ITS NOT A RIGHT ANSWER ");
    }
}
compareAnswers() {
    let newTotal = 0;
    for (var i = 0; i < correctAnswers.length; i++) {
        if (correctAnswers[i] === userAnswers[i]) {
            newTotal++;
        } else console.log("ITS NOT A RIGHT ANSWER ");
    }

    this.setState({Total: newTotal})
}
compareAnswers(){
设newTotal=0;
对于(变量i=0;i
您的尝试看起来不错,只是有语法错误(您不能这样做
Total:+1
)。相反,您可以使用函数式
setState
语法,对于您的代码段,该语法如下所示:

compareAnswers() {
    for (var i = 0; i < correctAnswers.length; i++) {
        if (correctAnswers[i] === userAnswers[i]) {
            this.setState(oldState => ({...oldState, Total: oldState.Total++}));
        } else console.log("ITS NOT A RIGHT ANSWER ");
    }
}
compareAnswers() {
    let newTotal = 0;
    for (var i = 0; i < correctAnswers.length; i++) {
        if (correctAnswers[i] === userAnswers[i]) {
            newTotal++;
        } else console.log("ITS NOT A RIGHT ANSWER ");
    }

    this.setState({Total: newTotal})
}
compareAnswers(){
对于(变量i=0;i({…oldState,Total:oldState.Total++});
}else console.log(“这不是一个正确的答案”);
}
}
但是,最好是对所有总和进行批处理,并在完成后只设置一次状态,如下所示:

compareAnswers() {
    for (var i = 0; i < correctAnswers.length; i++) {
        if (correctAnswers[i] === userAnswers[i]) {
            this.setState(oldState => ({...oldState, Total: oldState.Total++}));
        } else console.log("ITS NOT A RIGHT ANSWER ");
    }
}
compareAnswers() {
    let newTotal = 0;
    for (var i = 0; i < correctAnswers.length; i++) {
        if (correctAnswers[i] === userAnswers[i]) {
            newTotal++;
        } else console.log("ITS NOT A RIGHT ANSWER ");
    }

    this.setState({Total: newTotal})
}
compareAnswers(){
设newTotal=0;
对于(变量i=0;i
我的尝试基于以下假设,即所有问题都是强制性的,用户将按顺序回答:

compareAnswer() {
    // Addressing the best case i.e. when all user answers are correct
    if (JSON.stringify(this.state.correctAnswers) == JSON.stringify(this.state.userAnswers)) {
           this.setState({ Total: this.state.correctAnswers.length });
    } else {
           for (var i=0; i<this.state.userAnswers.length; i++) {
               if (this.state.correctAnswers[i] === this.state.userAnswers[i]) {
                  this.setState({ Total: this.state.Total++ });
               }
           }
    }
}
compareAnswer(){
//解决最佳情况,即当所有用户答案都正确时
if(JSON.stringify(this.state.correctAnswers)==JSON.stringify(this.state.userAnswers)){
this.setState({Total:this.state.correctAnswers.length});
}否则{

对于(var i=0;i我的尝试基于以下假设:所有问题都是强制性的,用户将按顺序回答:

compareAnswer() {
    // Addressing the best case i.e. when all user answers are correct
    if (JSON.stringify(this.state.correctAnswers) == JSON.stringify(this.state.userAnswers)) {
           this.setState({ Total: this.state.correctAnswers.length });
    } else {
           for (var i=0; i<this.state.userAnswers.length; i++) {
               if (this.state.correctAnswers[i] === this.state.userAnswers[i]) {
                  this.setState({ Total: this.state.Total++ });
               }
           }
    }
}
compareAnswer(){
//解决最佳情况,即当所有用户答案都正确时
if(JSON.stringify(this.state.correctAnswers)==JSON.stringify(this.state.userAnswers)){
this.setState({Total:this.state.correctAnswers.length});
}否则{

对于(var i=0;i向您已有的对象添加一个方法:

let state={
正确答案:[1,2,3,1,2,1,3,3,4,3],
用户回答:[1,3,1,2,2,1,1,3,4,3],
总数:0,
evalScore(){
此.correctAnswers.forEach((答案,索引)=>{
if(answer==this.userAnswers[index]){
这个,总共++
}
})
}
}
state.evalScore();

console.log(state.Total);
向您已有的对象添加方法:

let state={
正确答案:[1,2,3,1,2,1,3,3,4,3],
用户回答:[1,3,1,2,2,1,1,3,4,3],
总数:0,
evalScore(){
此.correctAnswers.forEach((答案,索引)=>{
if(answer==this.userAnswers[index]){
这个,总共++
}
})
}
}
state.evalScore();

console.log(state.Total);
您可以使用
forEach
和三元条件:

state.Total += (c === state.correctAnswers[i] ? 1 : 0)
var状态={
正确答案:[1,2,3,1,2,1,3,3,4,3],
用户回答:[1,3,1,2,2,1,1,3,4,3],
总数:0
};
state.userAnswers.forEach((c,i)=>state.Total+=(c===state.correctAnswers[i]?1:0));
console.log(状态)

。作为控制台包装{max height:100%!important;top:0;}
您可以使用
forEach
和三元条件:

state.Total += (c === state.correctAnswers[i] ? 1 : 0)
var状态={
正确答案:[1,2,3,1,2,1,3,3,4,3],
用户回答:[1,3,1,2,2,1,1,3,4,3],
总数:0
};
state.userAnswers.forEach((c,i)=>state.Total+=(c===state.correctAnswers[i]?1:0));
console.log(状态)

.as控制台包装{max height:100%!important;top:0;}
为什么
Array.prototype.forEach.call(state.userAnswers
在本机数组上?
[].forEach
是一种不好的做法。为什么不简单地
state.userAnswers.forEach((c,i)=>state.Total+=(c==state.correctAnswers[i]?1:0));)
?为什么
Array.prototype.forEach.call(本机数组上的state.userAnswers
[].forEach
是不好的做法。为什么不简单地
state.userAnswers.forEach((c,i)=>state.Total+=(c==state.correctAnswers[i]?1:0));)
?它只是给我一个错误“无法读取未定义的”Wh的属性“correctAnswers”