这些例子主要是对array进行了测试,从中可以学到$continue,$break,$reverse等东西,需要测试哪个,将其释放出来即可。

 

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
  2. <html> 
  3.     <head> 
  4.         <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
  5.         <title>Untitled Document</title> 
  6.         <script src="prototype.js"></script> 
  7.         <script> 
  8.         function arrayTest(){ 
  9.              
  10.             var arr = [1, 2, 3, 4, 8, 5, 4, 3]; 
  11.            /** // 依次输出1,2,3,4,5,4,3 
  12.             arr.each(function(item, index){ 
  13.                 if (item < 6) { 
  14.                     alert(item); 
  15.                 } 
  16.                 else { 
  17.                     throw $continue; 
  18.                 } 
  19.             });*/ 
  20.              // 依次输出1,2,3,4 
  21.              arr.each( 
  22.              function (item, index) { 
  23.              if (item < 6) { 
  24.              alert(item); 
  25.              } else { 
  26.              throw $break; 
  27.              } 
  28.              } 
  29.              ); 
  30.             /**//* 
  31.              var arr = [1,2,3,4,5]; 
  32.              // arr = [1,2,3,4,5] 
  33.              arr.reverse(false); 
  34.              alert(arr.inspect()); 
  35.              // arr = [5,4,3,2,1] 
  36.              arr.reverse(); 
  37.              alert(arr.inspect()); 
  38.              var arr = [1,2,3,4,5]; 
  39.              arr.reverse(); 
  40.              // 返回2在arr中的序号:1 
  41.              var index = arr.indexOf(2); 
  42.              alert(index) 
  43.              var arr = [1,2,3,4,5]; 
  44.              // 不包含2和4,返回[1,3,5] 
  45.              var newArr = arr.without(2,4); 
  46.              alert(newArr.inspect()); 
  47.              var arr = [1,2,3,4,5]; 
  48.              arr.clear(); 
  49.              alert(arr[2]); 
  50.              var arr = [1,2,3,4,5]; 
  51.              var arr2 = [2,4,6,8,10]; 
  52.              //[ [1,2],[2,4],[3,6],[4,8],[5,10]] 
  53.              var newArr = arr.zip(arr2); 
  54.              // [[1,4],[4,16],[9,36],[16,64],[25,100]] 
  55.              var newArr2 = arr.zip( 
  56.              arr2, 
  57.              function (item) { 
  58.              var newItem = item.collect( 
  59.              function (item, index) { 
  60.              return item * item; 
  61.              } 
  62.              ); 
  63.              return newItem; 
  64.              } 
  65.              ); 
  66.              alert(newArr.inspect()); 
  67.              alert(newArr2.inspect()); 
  68.              var arr = [5,2,3,1,4]; 
  69.              // 将arr排序 
  70.              var arrarr = arr.sortBy( 
  71.              function (item, index) { 
  72.              return item; 
  73.              } 
  74.              ); 
  75.              arr.each( 
  76.              function (item, index) { 
  77.              alert(item); 
  78.              } 
  79.              ); 
  80.              var arr = [ 
  81.              {root: 1, square: 1}, 
  82.              {root: 2, square: 4}, 
  83.              {root: 3, square: 9}, 
  84.              {root: 4, square: 16}, 
  85.              {root: 5, square: 25} 
  86.              ]; 
  87.              var newArr = arr.pluck("square"); 
  88.              alert(newArr[4]); 
  89.              var arr = [1,2,3,4,5]; 
  90.              var ptArr = arr.partition( 
  91.              function (item, index) { 
  92.              return item%2 == 1; 
  93.              } 
  94.              ); 
  95.              var oddArr = ptArr[0]; 
  96.              var evenArr = ptArr[1]; 
  97.              oddArr.each( 
  98.              function (item) { 
  99.              alert(item); 
  100.              } 
  101.              ); 
  102.              evenArr.each( 
  103.              function (item) { 
  104.              alert(item); 
  105.              } 
  106.              ); 
  107.              var arr = [1,2,3,4,5]; 
  108.              // 最大值 
  109.              var max = -arr.min( 
  110.              function (item, index) { 
  111.              return -item; 
  112.              } 
  113.              ); 
  114.              // 最小值 
  115.              var min = arr.min(); 
  116.              alert(max); 
  117.              alert(min); 
  118.              var arr = [1,2,3,4,5]; 
  119.              // 最大值 
  120.              var max = arr.max(); 
  121.              // 最小值 
  122.              var min = -arr.max( 
  123.              function (item, index) { 
  124.              return -item; 
  125.              } 
  126.              ); 
  127.              alert(max); 
  128.              alert(min); 
  129.              // 求集合中每一个元素的平方 
  130.              Number.prototype.square = function () { 
  131.              return this * this; 
  132.              } 
  133.              var arr = [1,2,3,4,5]; 
  134.              var newArr = arr.invoke("square"); 
  135.              alert(newArr[4]); 
  136.              // 计算arr中所有数的乘积 
  137.              var factorial = arr.inject( 
  138.              1, 
  139.              function (accumulator, item, index) { 
  140.              return accumulator * item; 
  141.              } 
  142.              ); 
  143.              alert(factorial) 
  144.              // 判断集合中是否包含”2“ 
  145.              var inc = arr.include(2); 
  146.              alert(inc); 
  147.              var arr = ["12345", "abc2", "cde", "fgh", "132ba"]; 
  148.              var newArray = arr.grep( 
  149.              /2/, 
  150.              function (item, index) { 
  151.              return item + " contains 2"; 
  152.              } 
  153.              ) 
  154.              newArray.each( 
  155.              function (item) { 
  156.              alert(item); 
  157.              } 
  158.              ); 
  159.              var arr = [1,2,3,4,5]; 
  160.              // 返回集合中所有的奇数 
  161.              var oddArr = arr.findAll( 
  162.              function (item, index) { 
  163.              return item%2 == 1; 
  164.              } 
  165.              ); 
  166.              alert(oddArr[2]); 
  167.              // 返回第一个大于3的元素 
  168.              var ele = arr.find( 
  169.              function (item, index) { 
  170.              return (item > 3); 
  171.              } 
  172.              ); 
  173.              alert(ele); 
  174.              var newArr = arr.collect( 
  175.              function (item, index) { 
  176.              return item * (index + 1); 
  177.              } 
  178.              ); 
  179.              var hasNegative = arr.any( 
  180.              function (item, index) { 
  181.              return (item < 0); 
  182.              } 
  183.              ); 
  184.              // 测试集合中的所有元素是否大于0 
  185.              var allPositive = arr.all( 
  186.              function(item, index) { 
  187.              return (item > 0); 
  188.              } 
  189.              ) 
  190.              alert(allPositive); 
  191.              arr.each( 
  192.              function (item, index) { 
  193.              alert("arr[" + index + "] = " + item); 
  194.              } 
  195.              );*/ 
  196.         } 
  197.         </script> 
  198.     </head> 
  199.     <body onload="arrayTest()"> 
  200.     </body> 
  201. </html>