每天一篇javascript学习小结(Array数组)
1、数组常用方法
varcolors=["red","blue","green"];//createsanarraywiththreestrings alert(colors.toString());//red,blue,green alert(colors.valueOf());//red,blue,green alert(colors);//red,blue,green
2、数组map()方法
varnumbers=[1,2,3,4,5,4,3,2,1];
varmapResult=numbers.map(function(item,index,array){
//item数组元素index元素对应索引array原数组
console.log(array===numbers);//true
returnitem*2;
});
console.log(mapResult);//[2,4,6,8,10,8,6,4,2]
3、数组reduce()方法
varvalues=[1,2,3,4,5];
//接收一个函数,然后从左到右遍历item,直到reduce到一个值。
varsum=values.reduce(function(prev,cur,index,array){
console.log(array===values);
console.log(index);//1,2,3,4数组的索引从1开始
returnprev+cur;//前后两个值相加
});
alert(sum);//15
4、数组concat()方法
//concat()方法用于连接两个或多个数组。
//该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本。
//语法
//arrayObject.concat(arrayX,arrayX,......,arrayX)
varcolors=["red","green","blue"];
varcolors2=colors.concat("yellow",["black","brown"]);
alert(colors);//red,green,blue
alert(colors2);//red,green,blue,yellow,black,brown
5、数组长度length
varcolors=newArray(3);//createanarraywiththreeitems
varnames=newArray("Greg");//createanarraywithoneitem,thestring"Greg"
alert(colors.length);//3
alert(names.length);//1
varcolors=["red","blue","green"];//createsanarraywiththreestrings varnames=[];//createsanemptyarray varvalues=[1,2,];//AVOID!Createsanarraywith2or3items varoptions=[,,,,,];//AVOID!createsanarraywith5or6items alert(colors.length);//3 alert(names.length);//0 alert(values.length);//2(FF,Safari,Opera)or3(IE) alert(options.length);//5(FF,Safari,Opera)or6(IE)
varcolors=["red","blue","green"];//createsanarraywiththreestrings colors.length=2; alert(colors[2]);//undefined
varcolors=["red","blue","green"];//createsanarraywiththreestrings colors.length=4; alert(colors[3]);//undefined
varcolors=["red","blue","green"];//createsanarraywiththreestrings colors[colors.length]="black";//addacolor colors[colors.length]="brown";//addanothercolor alert(colors.length);//5 alert(colors[3]);//black alert(colors[4]);//brown
varcolors=["red","blue","green"];//createsanarraywiththreestrings colors[99]="black";//addacolor(position99) alert(colors.length);//100
6、数组方法every和some
//every()与some()方法都是JS中数组的迭代方法。
//every()是对数组中的每一项运行给定函数,如果该函数对每一项返回true,则返回true。
//some()是对数组中每一项运行指定函数,如果该函数对任一项返回true,则返回true。
varnumbers=[1,2,3,4,5,4,3,2,1];
vareveryResult=numbers.every(function(item,index,array){
return(item>2);
});
alert(everyResult);//false
varsomeResult=numbers.some(function(item,index,array){
return(item>2);
});
alert(someResult);//true
7、数组filter()方法
//从数组中找到适合条件的元素(比如说大于某一个元素的值)
varnumbers=[1,2,3,4,5,4,3,2,1];
varfilterResult=numbers.filter(function(item,index,array){
return(item>2);
});
alert(filterResult);//[3,4,5,4,3]
8、数组indexOf和lastIndexOf
//indexOf()方法可返回某个指定的字符串值在字符串中首次出现的位置。
//语法
//stringObject.indexOf(searchvalue,fromindex)
//searchvalue必需。规定需检索的字符串值。
//fromindex可选的整数参数。规定在字符串中开始检索的位置。它的合法取值是0到stringObject.length-1。如省略该参数,则将从字符串的首字符开始检索。
/*
lastIndexOf()方法可返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索。
语法
stringObject.lastIndexOf(searchvalue,fromindex)
searchvalue必需。规定需检索的字符串值。
fromindex可选的整数参数。规定在字符串中开始检索的位置。它的合法取值是0到stringObject.length-1。如省略该参数,则将从字符串的最后一个字符处开始检索。
*/
varnumbers=[1,2,3,4,5,4,3,2,1];
alert(numbers.indexOf(4));//3
alert(numbers.lastIndexOf(4));//5
alert(numbers.indexOf(4,4));//5
alert(numbers.lastIndexOf(4,4));//3
varperson={name:"Nicholas"};
varpeople=[{name:"Nicholas"}];
varmorePeople=[person];
alert(people.indexOf(person));//-1
alert(morePeople.indexOf(person));//0
9、数组toLocaleString和toString
varperson1={
toLocaleString:function(){
return"Nikolaos";
},
toString:function(){
return"Nicholas";
}
};
varperson2={
toLocaleString:function(){
return"Grigorios";
},
toString:function(){
return"Greg";
}
};
varpeople=[person1,person2];
alert(people);//Nicholas,Greg
alert(people.toString());//Nicholas,Greg
alert(people.toLocaleString());//Nikolaos,Grigorios
10、数组push和pop方法
varcolors=newArray();//createanarray
varcount=colors.push("red","green");//pushtwoitems
alert(count);//2
count=colors.push("black");//pushanotheritemon
alert(count);//3
varitem=colors.pop();//getthelastitem
alert(item);//"black"
alert(colors.length);//2
11、数组方法unshift和shift
//unshift()方法可向数组的开头添加一个或更多元素,并返回新的长度。
//shift()方法用于把数组的第一个元素从其中删除,并返回第一个元素的值。
varcolors=newArray();//createanarray
varcount=colors.unshift("red","green");//pushtwoitems
alert(count);//2
count=colors.unshift("black");//pushanotheritemon
alert(count);//3
varitem=colors.pop();//getthefirstitem
alert(item);//"green"
alert(colors.length);//2
12、数组倒序方法reverse
varvalues=[1,2,3,4,5]; values.reverse(); alert(values);//5,4,3,2,1
13、数组排序方法sort
functioncompare(value1,value2){
if(value1<value2){
return-1;
}elseif(value1>value2){
return1;
}else{
return0;
}
}
varvalues=[0,1,16,10,15];
values.sort(compare);
alert(values);//0,1,10,15,16
//sort改变原数组
14、数组方法slice
/* slice()方法可从已有的数组中返回选定的元素。 语法 arrayObject.slice(start,end) start必需。规定从何处开始选取。如果是负数,那么它规定从数组尾部开始算起的位置。也就是说,-1指最后一个元素,-2指倒数第二个元素,以此类推。 end可选。规定从何处结束选取。该参数是数组片断结束处的数组下标。如果没有指定该参数,那么切分的数组包含从start到数组结束的所有元素。如果这个参数是负数,那么它规定的是从数组尾部开始算起的元素。 返回值 返回一个新的数组,包含从start到end(不包括该元素)的arrayObject中的元素。 */ varcolors=["red","green","blue","yellow","purple"]; varcolors2=colors.slice(1); varcolors3=colors.slice(1,4); alert(colors2);//green,blue,yellow,purple alert(colors3);//green,blue,yellow
15、数组方法splice
/* plice()方法向/从数组中添加/删除项目,然后返回被删除的项目。 注释:该方法会改变原始数组。 语法 arrayObject.splice(index,howmany,item1,.....,itemX) index必需。整数,规定添加/删除项目的位置,使用负数可从数组结尾处规定位置。 howmany必需。要删除的项目数量。如果设置为0,则不会删除项目。 item1,...,itemX可选。向数组添加的新项目。 */ varcolors=["red","green","blue"]; varremoved=colors.splice(0,1);//removethefirstitem alert(colors);//green,blue alert(removed);//red-oneitemarray removed=colors.splice(1,0,"yellow","orange");//inserttwoitemsatposition1 alert(colors);//green,yellow,orange,blue alert(removed);//emptyarray removed=colors.splice(1,1,"red","purple");//inserttwovalues,removeone alert(colors);//green,red,purple,orange,blue alert(removed);//yellow-oneitemarray
16、数组isArray()方法
alert(Array.isArray([]));//true
alert(Array.isArray({}));//false
以上就是今天的javascript学习小结,之后每天还会继续更新,希望大家继续关注。