comparing two javascript arrays?

前端 未结 3 1150
情歌与酒
情歌与酒 2021-01-29 10:47

I want to compare two arrays with each other and see if there is a match, and if there is do something.

var answers = new Array(\"a\",\"b\",\"c\",\"d\", \"e\");
         


        
3条回答
  •  臣服心动
    2021-01-29 11:49

    Try using this:

    for(var i = 0; i < answers.length; i++) {
        for(var j = 0; j < correct.length; j++){
            if (answers[i] === correct[j]){ 
                console.log(answers[i]+ " is the correct answer")
                break;
            }
        }
    }
    

提交回复
热议问题