zoukankan      html  css  js  c++  java
  • 案例: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>
  • 相关阅读:
    Atom 和 markdown 基本使用
    c++11 正则表达式基本使用
    Emacs 之窗口管理
    Emacs 之列编辑模式
    Emacs 之查看帮助
    使用 json_in_java
    Linux服务 httpd
    Linux 编译安装BIND
    Kerberos
    Linux服务 DNS&BIND
  • 原文地址:https://www.cnblogs.com/qtbb/p/11819259.html
Copyright © 2011-2022 走看看