zoukankan      html  css  js  c++  java
  • JavaScript【引用方法】迭代方法

    以下为学习《JavaScript 高级程序设计》(第 3 版) 所做笔记。

     1 <script>
     2     var arr1 = [1,2,2,3,4,3,2,2,1];
     3 
     4     //every()
     5     //对数组中的每一项运行给定函数,如果对每一项都返回true,则返回true
     6     var everyRs = arr1.every(function(item, index, array){
     7         return(item>2);
     8     });
     9     console.log(everyRs);    //输出:false
    10 
    11     //some()
    12     //对数组中的每一项运行给定函数,如果对其中任何一项返回true,则返回true
    13     var someRs = arr1.some(function(item, index, array){
    14         return(item>2);
    15     });
    16     console.log(someRs);    //输出:true
    17 
    18     //filter()
    19     //对数组中的每一项运行给定函数,返回函数返回true的项组成的数组
    20     var filterRs = arr1.filter(function(item, index, array){
    21         return(item>2);
    22     });
    23     console.log(filterRs);    //输出:(3) [3, 4, 3]
    24 
    25     //map()
    26     //对数组中的每一项运行给定函数,返回每次函数调用的结果返回的数组
    27     var mapRs = arr1.map(function(item, index, array){
    28         return item*2;
    29     });
    30     console.log(mapRs);        //输出:(9) [2, 4, 4, 6, 8, 6, 4, 4, 2]
    31 
    32     //forEach
    33     //对数组中的每一项运行给定函数,没有返回值
    34     var forEachRs = arr1.forEach(function(item, index, array){
    35         //执行某些操作
    36         console.log(item + ' => ' + index + ' => ' + array);
    37         //输出:
    38         // 1 => 0 => 1,2,2,3,4,3,2,2,1
    39         // 2 => 1 => 1,2,2,3,4,3,2,2,1
    40         // 2 => 2 => 1,2,2,3,4,3,2,2,1
    41         // 3 => 3 => 1,2,2,3,4,3,2,2,1
    42         // 4 => 4 => 1,2,2,3,4,3,2,2,1
    43         // 3 => 5 => 1,2,2,3,4,3,2,2,1
    44         // 2 => 6 => 1,2,2,3,4,3,2,2,1
    45         // 2 => 7 => 1,2,2,3,4,3,2,2,1
    46         // 1 => 8 => 1,2,2,3,4,3,2,2,1
    47     });
    48 </script>
  • 相关阅读:
    排序算法之——冒泡排序优化
    Linux程序在预处理、编译、汇编、链接、运行步骤的作用
    理解可变参数的原理
    对虚函数、虚表的认识
    成员函数的重载、覆盖、隐藏
    centOS7-mariadb的安装
    centOS7-本地源配置
    vmware中桥接、NET、仅主机模式详解
    XXX系统项目目标文档课堂讨论
    做生活的有心人——xxx系统第一阶段总结
  • 原文地址:https://www.cnblogs.com/xiaoxuStudy/p/12304220.html
Copyright © 2011-2022 走看看