W3School TIY Editor
W3School 在线教程
改变方向
暗黑模式
运行代码
<!DOCTYPE html> <html> <body> <h1>JavaScript 数组</h1> <p>检查数组中的所有答案是否相同。</p> <p id="demo"></p> <script> const survey = [ { name: "Bill", answer: "Yes"}, { name: "Steve", answer: "Yes"}, { name: "David", answer: "Yes"}, { name: "Elon", answer: "No"} ]; document.getElementById("demo").innerHTML = survey.every(isSameAnswer); function isSameAnswer(el,index,arr) { // 为第一个元素返回 true if (index === 0){ return true; } else { // 比较前一个元素的值 return (el.answer === arr[index - 1].answer); } } </script> </body> </html>