前两天看的文章里刚刚说ECMA 262中的一些String特性,又正好看到一篇博客介绍一些其他操作的实现,比如foreach,这个对于PHP用户来说就太常用了,而以前在 javascript中只能用for(i in arr)来实现,虽然都没有问题,但毕竟看起来怪怪的。
上次的文章在这里:Javascript中各种trim的实现 ,这次的文章引用自Javascript1.6数组新特性和jquery的几个工具方法 。
原文如下,其中会插入自己的内容,会特别标记
JavaScript 1.6 引入了几个新的Array 方法,具体的介绍见:New in JavaScript 1.6 。这些方法已经被写进了ECMA262 V5。现代浏览器(IE9/Firefox/Safari/Chrome/Opera)都已经支持,但IE6/7/8不支持。jquery的工具方法中提供了类似的功能。【膘叔:上次在讲trim的时候自己也测试过,确实IE8不支持,低于IE8的就更不用想了】
1、Array.forEach()和jquery的$().each()。 在数组中的每个项上运行一个函数。类似java5 增强的for循环买卖IC网 。
view plaincopy to clipboardprint?
var ary = [2,4,6,8];     
// js1.6 Array.forEach方法     
ary.forEach(function(i){alert(i);});     
// jquery的写法     
$(ary).each(function(){alert(this);});     
//还可以写成这样     
$(ary).each(function(index,item){alert(item);});//index是元素的索引,item是该元素    
//膘叔:这个相当于PHP中如下代码。。   
while (list($key, $val) = each($fruit)) {   
    echo "$key => $val\n";   
}  
var ary = [2,4,6,8];  
// js1.6 Array.forEach方法  
ary.forEach(function(i){alert(i);});  
// jquery的写法  
$(ary).each(function(){alert(this);});  
//还可以写成这样  
$(ary).each(function(index,item){alert(item);});//index是元素的索引,item是该元素 
//膘叔:这个相当于PHP中如下代码。。
while (list($key, $val) = each($fruit)) {
    echo "$key => $val\n";
}
2、Array.filter()和jquery的$.grep()。 在数组中的每个项上运行一个函数,并将函数返回真值的项作为数组返回。简单的说就是用一个条件过滤掉不符合的数组元素,剩下的符合条件的元素组合成新的数组返回。
view plaincopy to clipboardprint?
var ary = [2,4,6,8];     
// js1.6 Array.filter()方法     
var otherAry1 = ary.filter(function(item){return item>4;});     
alert(otherAry1);//输出6,8     
// jquery写法(注意和$.each的区别)     
// 此处你们函数中第一个参数是数组元素自身,第二个参数是数组元素索引     
// 而$().each方法刚好相反,作者应该统一下。     
var otherAry2 = $.grep(ary,function(item,index){return item>4;});     
alert(otherAry2);//输出6,8    
//膘叔:PHP中有类似函数,比如array_filter,也是可以通过callback来处理  
var ary = [2,4,6,8];  
// js1.6 Array.filter()方法  
var otherAry1 = ary.filter(function(item){return item>4;});  
alert(otherAry1);//输出6,8  
// jquery写法(注意和$.each的区别)  
// 此处你们函数中第一个参数是数组元素自身,第二个参数是数组元素索引  
// 而$().each方法刚好相反,作者应该统一下。  
var otherAry2 = $.grep(ary,function(item,index){return item>4;});  
alert(otherAry2);//输出6,8 
//膘叔:PHP中有类似函数,比如array_filter,也是可以通过callback来处理
3、Array.map()和jquery的$.map()。 在数组中的每个项上运行一个函数,并将全部结果作为数组返回。这个方法非常强大,尤其是作用于DOM数组时(在abcc项目上用过,对每个查询模块DOM生成查询字符串)。简单说就是把每个数组元素运算的结果作为新数组元素(还是很拗口)【膘叔:php中有array_map,array_walk等】
view plaincopy to clipboardprint?
var ary = [2,4,6,8];     
// js1.6 Array.map()方法     
var newAry1 = ary.map(function(item){return item+1;});//每个元素加1     
alert(newAry1);//输出3,5,7,9     
// jquery写法   
var newAry2 = $.map(ary,function(item,index){return item+1;});     
alert(newAry2);//输出3,5,7,9  
var ary = [2,4,6,8];  
// js1.6 Array.map()方法  
var newAry1 = ary.map(function(item){return item+1;});//每个元素加1  
alert(newAry1);//输出3,5,7,9  
// jquery写法
var newAry2 = $.map(ary,function(item,index){return item+1;});  
alert(newAry2);//输出3,5,7,9
4、Array.every()方法。 检查数组元素是否都符合某个条件,只要有一个不符合返回false,否则返回true
view plaincopy to clipboardprint?
var ary = [2,4,6,8,10];     
alert(ary.every(function(item){return item>1}));//true     
alert(ary.every(function(item){return item>2}));//false  
var ary = [2,4,6,8,10];  
alert(ary.every(function(item){return item>1}));//true  
alert(ary.every(function(item){return item>2}));//false
5、Array.some()方法。 检查数组中元素是否符合某个条件,只要有一个符合返回true,否则返回false
view plaincopy to clipboardprint?
var ary = [2,4,,6,8,10];     
alert(ary.some(function(item){return item>9;}));//true     
alert(ary.some(function(item){return item>10;}));//false  
var ary = [2,4,,6,8,10];  
alert(ary.some(function(item){return item>9;}));//true  
alert(ary.some(function(item){return item>10;}));//false
最后给出 IE6/7/8的解决方案 ,让这些浏览器完美支持JS1.6的Array新方法。
view plaincopy to clipboardprint?
 1. -function(){     
 2.          
 3. function applyIf(o, c) {     
 4.     if(o) {     
 5.         for(var p in c) {     
 6.             if(o[p]===undefined) {     
 7.                 o[p] = c[p];     
 8.             }     
 9.         }     
10.     }     
11.     return o;     
12. }     
13. applyIf(Array.prototype, {     
14.     indexOf : function(obj, idx) {     
15.         var from = idx == null ? 0 : (idx < 0 ? Math.max(0, arr.length + idx) : idx);     
16.         for(var i = from, l = this.length; i < l; i++) {     
17.             if(i in this && this[i] === obj) {     
18.                 return i;     
19.             }     
20.         }     
21.         return -1;     
22.     },     
23.     lastIndexOf : function(obj, idx) {     
24.         var len = this.length, from = idx == null ? len - 1 : idx;     
25.         if(from < 0) {     
26.             from = Math.max(0, len + from);     
27.         }     
28.         for(var i = from; i >= 0; i--) {     
29.             if (i in this && this[i] === obj) {     
30.                 return i;     
31.             }     
32.         }     
33.         return -1;     
34.     },     
35.     every : function(fn, thisObj) {     
36.         var l = this.length;     
37.         for(var i = 0; i < l; i++) {     
38.             if(i in this && !fn.call(thisObj, this[i], i, this)) {     
39.                 return false;     
40.             }     
41.         }     
42.         return true;     
43.     },     
44.     some : function(fn, thisObj) {     
45.         var l = this.length;     
46.         for(var i = 0; i < l; i++) {     
47.             if(i in this && fn.call(thisObj, this[i], i, this)) {     
48.                 return true;     
49.             }     
50.         }     
51.         return false;     
52.     },     
53.     filter : function(fn, thisObj) {     
54.         var l = this.length, res = [], resLength = 0;     
55.         for(var i = 0; i < l; i++) {     
56.             if(i in this) {     
57.                 var val = this[i];     
58.                 if(fn.call(thisObj, val, i, this)) {     
59.                     res[resLength++] = val;     
60.                 }     
61.             }     
62.         }     
63.         return res;     
64.     },     
65.     map : function(fn, thisObj) {     
66.         var l = this.length, res = [];     
67.         for(var i = 0; i < l; i++) {     
68.             if(i in this) {     
69.                 res[i] = fn.call(thisObj, this[i], i, this);     
70.             }     
71.         }     
72.         return res;     
73.     },     
74.     forEach : function(fn, thisObj) {     
75.         var l = this.length;     
76.         for(var i = 0; i < l; i++) {     
77.             if(i in this) {     
78.                 fn.call(thisObj, this[i], i, this);     
79.             }     
80.         }     
81.     }     
82. });      
83. }();    
   1. -function(){  
   2.       
   3. function applyIf(o, c) {  
   4.     if(o) {  
   5.         for(var p in c) {  
   6.             if(o[p]===undefined) {  
   7.                 o[p] = c[p];  
   8.             }  
   9.         }  
  10.     }  
  11.     return o;  
  12. }  
  13. applyIf(Array.prototype, {  
  14.     indexOf : function(obj, idx) {  
  15.         var from = idx == null ? 0 : (idx < 0 ? Math.max(0, arr.length + idx) : idx);  
  16.         for(var i = from, l = this.length; i < l; i++) {  
  17.             if(i in this && this[i] === obj) {  
  18.                 return i;  
  19.             }  
  20.         }  
  21.         return -1;  
  22.     },  
  23.     lastIndexOf : function(obj, idx) {  
  24.         var len = this.length, from = idx == null ? len - 1 : idx;  
  25.         if(from < 0) {  
  26.             from = Math.max(0, len + from);  
  27.         }  
  28.         for(var i = from; i >= 0; i--) {  
  29.             if (i in this && this[i] === obj) {  
  30.                 return i;  
  31.             }  
  32.         }  
  33.         return -1;  
  34.     },  
  35.     every : function(fn, thisObj) {  
  36.         var l = this.length;  
  37.         for(var i = 0; i < l; i++) {  
  38.             if(i in this && !fn.call(thisObj, this[i], i, this)) {  
  39.                 return false;  
  40.             }  
  41.         }  
  42.         return true;  
  43.     },  
  44.     some : function(fn, thisObj) {  
  45.         var l = this.length;  
  46.         for(var i = 0; i < l; i++) {  
  47.             if(i in this && fn.call(thisObj, this[i], i, this)) {  
  48.                 return true;  
  49.             }  
  50.         }  
  51.         return false;  
  52.     },  
  53.     filter : function(fn, thisObj) {  
  54.         var l = this.length, res = [], resLength = 0;  
  55.         for(var i = 0; i < l; i++) {  
  56.             if(i in this) {  
  57.                 var val = this[i];  
  58.                 if(fn.call(thisObj, val, i, this)) {  
  59.                     res[resLength++] = val;  
  60.                 }  
  61.             }  
  62.         }  
  63.         return res;  
  64.     },  
  65.     map : function(fn, thisObj) {  
  66.         var l = this.length, res = [];  
  67.         for(var i = 0; i < l; i++) {  
  68.             if(i in this) {  
  69.                 res[i] = fn.call(thisObj, this[i], i, this);  
  70.             }  
  71.         }  
  72.         return res;  
  73.     },  
  74.     forEach : function(fn, thisObj) {  
  75.         var l = this.length;  
  76.         for(var i = 0; i < l; i++) {  
  77.             if(i in this) {  
  78.                 fn.call(thisObj, this[i], i, this);  
  79.             }  
  80.         }  
  81.     }  
  82. });   
  83. }();  
------EOF----
最后这一段有事没事可以参考一下哦,毕竟这些都是可以拿来做参考的,比如象最后一个foreach,可以好好学学这些代码哦。
最后PDF目录,var evens = [i for each (i in numbers) if (i % 2 == 0)];,看到这个,有没有感觉很象python的写法???这个是javascript 1.7中的写法了,看看这里的介绍:https://developer.mozilla.org/en/JavaScript/Guide /Predefined_Core_Objects#Working_with_Array-like_objects