• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
小许学习笔记
博客园    首页    新随笔    联系   管理    订阅  订阅
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>

 

posted on 2020-02-13 17:02  xiaoxustudy  阅读(141)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3