Dojo中数组arrayUtil的几个简单方法示例

懒得写文字,直接贴index.html代码:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta charset="UTF-8" >
 5     <title>Tutorial: Hello Dojo!</title>
 6 </head>
 7 
 8 <body>
 9     <h1 id="greeting">Hello</h1>
10 
11     <script type="text/javascript">
12         var dojoConfig = {
13             async : true,
14           //baseUrl : '.'
15             packages : [{
16                 name : "dojo",
17                 location : location.pathname.replace(/\/[^/]*$/,'')+"/../dojo-release-1.10.4/dojo"
18             }]
19         }
20     </script>
21 
22     <!-- 加载Dojo -->
23     <script src="../dojo-release-1.10.4/dojo/dojo.js"></script>
24 
25     <script>
26         require([
27             'dojo/_base/array'
28         ], function (arrayUtil) {
29 
30             //********************原始数组*****************//
31             var arr = [1,2,3,4,3,2,1,2,3,4,3,2,1];
32 
33             //****************indexOf(),lastIndexOf()*************//
34             var arr1 = arrayUtil.indexOf(arr, 2);
35             var arr2 = arrayUtil.indexOf(arr, 2, 2);
36             var arr3 = arrayUtil.lastIndexOf(arr, 2);
37 
38             console.group("indexOf(),lastIndexOf()");
39             console.log(arr1);        //1
40             console.log(arr2);        //5
41             console.log(arr3);        //11
42             console.groupEnd();
43 
44             //**********************forEach()*********************//
45             console.group("forEach()");
46             arrayUtil.forEach(arr,function(item,index){
47                 console.log(""+index+"元素为:"+item);
48             })
49             console.groupEnd();
50 
51             //********************map() filter()******************//
52             console.group("map(),filter()");
53             var arr4 = arrayUtil.map(arr,function(item,index){
54                 return item + 10;
55             })
56             var arr5 = arrayUtil.filter(arr,function(item,index){
57                 return item%2 == 0;
58             })
59             console.log(arr4);    //[11, 12, 13, 14, 13, 12, 11, 12, 13, 14, 13, 12, 11]
60             console.log(arr5);    //[2, 4, 2, 2, 4, 2]
61             console.groupEnd();
62 
63             //******************every() some()*******************//
64             console.group("erery(),some()");
65             var isAllIsTwo = arrayUtil.every(arr,function(item){
66                 return item == 2;
67             });
68             var isExistTwo = arrayUtil.some(arr,function(item){
69                 return item ==2;
70             });
71             console.log(isAllIsTwo);    //false
72             console.log(isExistTwo);    //true
73             console.groupEnd();
74         });
75     </script>
76 </body>
77 </html>

 文件夹结构如下:

--myTestDemos
    |--array
        |--index.html
    |--dojo-release-1.10.4
        |--dojo
        |--dijit
        |--dojox

 

posted @ 2015-09-18 18:37  奈河桥  阅读(489)  评论(0编辑  收藏  举报