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

时间:2018-03-02 01:46:15

标签: javascript reactjs

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

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

如何检查两者在索引上是否具有相同的值,如果是,则在Total

添加+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 ");
    }
   }

5 个答案:

答案 0 :(得分:3)

好的,因为它是一个对象内部的数组,而实际对象有一个this引用,我们必须适应它。

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

使用此方法,我们可以动态比较两个数组。希望这有帮助!

答案 1 :(得分:1)

您的尝试看起来很不错,只是它有语法错误(您不能这样做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})
}

答案 2 :(得分:1)

我的尝试基于以下假设:所有问题都是强制性的,并且将由用户按顺序回答:

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++ });
               }
           }
    }
}

答案 3 :(得分:1)

向您已拥有的对象添加方法:

&#13;
&#13;
let 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,
  evalScore() {
    this.correctAnswers.forEach((answer, index) => {
        if (answer === this.userAnswers[index]) {
          this.Total++
        }
    })
  }
}

state.evalScore();
console.log(state.Total);
&#13;
&#13;
&#13;

您甚至可以使用Total属性并使其像计算属性一样工作:

&#13;
&#13;
let state = {
  correctAnswers: [1, 2, 3, 1, 2, 1, 3, 3, 4, 3],
  userAnswers: [1, 3, 1, 2, 2, 1, 1, 3, 4, 3],
  Total() {
    // make sure both array have the same number of elements
    if(this.correctAnswers.length !== this.userAnswers.length) {
      return null;
    }
    let total = 0
    this.correctAnswers.forEach((answer, index) => {
        if (answer === this.userAnswers[index]) {
          total++
        }
    })
    return total
  }
}

console.log(state.Total());
&#13;
&#13;
&#13;

修改:Google Chrome屏幕截图

Google Chrome Screenshot

答案 4 :(得分:1)

您可以使用forEach和三元条件:

state.Total += (c === state.correctAnswers[i] ? 1 : 0)

&#13;
&#13;
var 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
};

state.userAnswers.forEach((c, i) => state.Total += (c === state.correctAnswers[i] ? 1 : 0));

console.log(state)
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;