Array 扩展

each扩展,类似Hash的each,可以参见Hash.html

var array=["First","Second","Third"];
array.each(function(str,index){alert(index+":"+str);})

在数组比较大的时候,不推荐使用each方法,这是由于each的性能比较低

Array的clear(),clone(),compact(),uniq(),without(),first(),last(),indexOf(),inspect(),reverse(),size()

var guys=['Sam','Justin','Andrew','Dan','Sam','sam',null,''];
guys.clear();//清空所有元素,返回空数组
var myGuys=guys.clone();// myGuys->['Sam','Justin','Andrew','Dan','Sam','sam',null,''];  复制数组
var myGuys=guys.compact();// myGuys->['Sam','Justin','Andrew','Dan','Sam','sam','']; 去除null
var myGuys=guys.uniq();// myGuys->['Sam','Justin','Andrew','Dan','sam',''] 去除相同元素,区分大小写

var mySubGuys=guys.without("Justin");// mySubGuys->['Sam','Andrew','Dan','Sam','sam',null,''];去除特定元素
var mySubGuys=guys.without("Justin","Dan");// mySubGuys->['Sam','Andrew','Sam','sam',null,''];可以带多个参数

var Sam=guys.first();// Sam->"Sam" 第一个元素
var NoName=guys.last();//MoName->""最后一个元素


['frank',['bob','lisa'],['jill',['tom','sally']]].flatten()
//->['frank','bob','lisa','jill','tom','sally'] 将多维数组转化为一维数组

[3,5,6,1,20].indexOf(1)
//->3
[3,5,6,1,20].indexOf(90)
//->-1
[0,false,15].indexOf(false)
//->0 instead of 1,because 0==false!
//indexOf方法在判断是否相等时使用的操作符为"==",不是严格的"==="

['Apples',{good:'yes',bad:'no'},3,34].inspect()
//->"['Apples',[objectObject],3,34]"
//任何Object都有inspect方法,参见Object.html

[1,2,3,4].reverse();//->[4,3,2,1]

[1,2,3,4].size();//->4

Array.from(iterable)->new Array功能和 $A()一样