簡體   English   中英

如何檢查this.state中的兩個數組是否包含相同的值?

[英]How Can I check if both Arrays inside this.state contains same value?

我有兩個數組,其中一個包含正確答案的索引號correctAnswers

並且userAnswers包含用戶選擇的答案的索引號

如何檢查兩個索引的值是否相同,如果是,則將+1加到Total

    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應該為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 ");
    }
   }

好的,因為它是對象內部的數組,並且實際對象具有this引用,所以我們必須容納this引用。

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

使用此方法,我們可以動態比較兩個數組。 希望這可以幫助!

您的嘗試看起來不錯,除了它有語法錯誤(您不能這樣做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})
}

我的嘗試是基於所有問題都是強制性的,並且用戶會依次回答的假設:

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

將方法添加到您已經擁有的對象中:

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

您甚至可以為此使用Total屬性,並使它像計算屬性一樣工作:

 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()); 

編輯:谷歌瀏覽器截圖

Google Chrome屏幕截圖

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

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

 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) 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM