案例:forEach和some区别

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
  </head>

  <body>
    <script>
      var arr = ["red", "green", "blue", "pink", "red"];
      // 1. forEach迭代 遍历
      // arr.forEach(function(value) {
      //     if (value == 'green') {
      //         console.log('找到了该元素');
      //         return true; // 在forEach 里面 return 不会终止迭代
      //     }
      //     console.log(11);

      // })
      // 如果查询数组中唯一的元素, 用some方法更合适,
      arr.some(function(value) {
        if (value == "red") {
          console.log("找到了该元素:" + value);
          return true; //  在some 里面 遇到 return true 就是终止遍历 迭代效率更高
        }
        console.log(11);
      });
      // arr.filter(function(value) {
      //     if (value == 'green') {
      //         console.log('找到了该元素');
      //         return true; //  // filter 里面 return 不会终止迭代
      //     }
      //     console.log(11);

      // });
    </script>
  </body>
</html>
原文地址:https://www.cnblogs.com/qtbb/p/11819259.html