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>
  • 相关阅读:
    hdu 1896 stones
    各种类型的取值范围
    RSS/PSS/VSZ
    kasan BUG log
    ARM机器码分析
    Linux进程状态
    谢宝友: 深入理解RCU之七:分级RCU实现
    rcu_preempt detected stalls on CPUs/tasks
    Linux 内核 hlist
    linux cmd
  • 原文地址:https://www.cnblogs.com/qtbb/p/11819259.html
Copyright © 2011-2022 走看看