标签:
1 var ary1 = new Array();//空数组 2 var ary2= [] ;//字面量
//方法一 if(array instanceof Array){ } //方法二 ES5 if(Array.isArray(array)){ } //方法三 Object.prototype.toString.apply(arrary,arguments);
//1、toString(); var ary =[‘aaaa‘,‘vvvvv‘]; ary.toString();//"aaaa,vvvvv" //2、valueOf(); var ary =[‘green‘,‘blue‘,‘red‘]; ary.valueOf();//["green", "blue", "red"] //3、toLocaleString(); ary.toLocaleString();//"green,blue,red" //4、join(); ary.join(‘@@@&&&&&*****‘) //"green@@@&&&&&*****blue@@@&&&&&*****red"
‘后进先出’的表现
ary =[‘aa‘,‘bbb‘,‘cccc‘,‘dddd‘]; //["aa", "bbb", "cccc", "dddd"] ary.pop(); //"dddd" ary.push(‘dddd‘); //4 ary //["aa", "bbb", "cccc", "dddd"]
‘先进先出’的表现
var ary = ["aa", "bbb", "cccc", "dddd"]; ary.shift();//"aa" ary.unshift(‘aaaaaaaaaaaa‘);//4 console.log(ary);//["aaaaaaaaaaaa", "bbb", "cccc", "dddd"]
1、从后到前
var ary =[10,20,30,40,50,60,70,80,90]; //从后到前 //ary.unshift(ary.pop()); for(var i = 0 ,len =ary.length; i<len;i++){ console.log(ary); ary.unshift(ary.pop()); console.log(ary); }
2、从前到后
for(var i = 0 ,len =ary.length; i<len;i++){ console.log(ary); ary.push(ary.shift()); console.log(ary); }
var ary =[90,100,90,33300,10,20,30,40,50,60,70,80,90]; ary.sort(function(val1,val2){ return val1-val2; }); //[10, 20, 30, 40, 50, 60, 70, 80, 90, 90, 90, 100, 33300] ary.sort(function(val1,val2){ return val2-val1; }); //[33300, 100, 90, 90, 90, 80, 70, 60, 50, 40, 30, 20, 10]
1、concat();
ary.concat(‘aaa‘,[‘ddd‘,‘ccc‘,‘eeee‘]); //[33300, 100, 90, 90, 90, 80, 70, 60, 50, 40, 30, 20, 10, "aaa", "ddd", "ccc", "eeee"]
2、slice();
var ary1 = [33300, 100, 90, 90, 90, 80, 70, 60, 50, 40, 30, 20, 10, "aaa", "ddd", "ccc", "eeee"]; ary1.slice(0,5);//[33300, 100, 90, 90, 90] console.log(ary1);//[33300, 100, 90, 90, 90, 80, 70, 60, 50, 40, 30, 20, 10, "aaa", "ddd", "ccc", "eeee"] ary1.slice(1,3);//[100, 90]
3、splice();最强大的方法
var ary2 = [‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘aa‘,‘bb‘,‘cc‘] ary2 //["a", "b", "c", "d", "e", "aa", "bb", "cc"] ary2.splice(0,0); //[] ary2 //["a", "b", "c", "d", "e", "aa", "bb", "cc"] ary2.splice(1,3); //["b", "c", "d"] ary2 //["a", "e", "aa", "bb", "cc"] ary2.splice(0,0,‘a‘,‘aaaa‘); //[] ary2 //["a", "aaaa", "a", "e", "aa", "bb", "cc"]
ary1 = [‘222‘,‘ffff‘,‘sfff‘,‘a‘] //["222", "ffff", "sfff", "a"] ary1.indexOf(‘a‘); //3 ary1.indexOf(‘aaaaa‘); //-1
1.every();数组所有值返回true 返回true
2.some();数组其中有返回true 返回true
3.forEach();数组每一项运行函数,没有返回值
4.map();数组每一项运行函数,返回数组
5.filter();数组每一项运行函数,返回 ture数组
var ary1 = [10,20,5,44,444]; ary1.some(function(value,index){ return value>100; });//ture ary1.every(function(value){ return value<0; });//false ary1.map(function(value,index){ return value+index; });//[10, 21, 7, 47, 448] ary1.filter(function(value,index){ return value>40; });//[44, 444] ary1.forEach(function(value,index){ ary1[index]=value*2; }); console.log(ary1);//[20, 40, 10, 88, 888]
标签:
原文地址:http://www.cnblogs.com/niunai007/p/5671901.html