js创建及操作数组

一:创建数组

1:使用Array构造函数(new可以省略):

var colors = new Array();
创建length值为20的的数组: var colors = new Array(20); 直接传递数组的项: var colors = new Array("red", "blue", "green");

2:使用数组字面量表示法:

 var colors = ["red", "blue", "green"];

3:读取和设置数组的值

colors[2] = "black"   //修改第三项

colors[3] = "brown"  //新增第四项

colors.length = 2;   //数组长度缩短为2

colors[colors.length] = "white" //设置最后一项

二:检测数组

1:一个全局作用域的情况下:

if(value instanceof Array){

  //执行某些操作

}

2:无视多个全局作用域,确定是否为数组:

if(Array.isArray(value)){

  //对数组执行某些操作(ie9+)

}

三:转换方法(toString(),valueOf(),join())

var colors = ["red", "blue", "green"];
 alert(colors.toString());   //red,blue,green以逗号分隔的字符串
 alert(colors.valueOf());    //red,blue,green返回的是数组
 alert(colors);              //red,blue,green

var colors2 = ["red", "blue", "green"];
alert(colors2.join(",")); //red,blue,green
alert(colors2.join("||")); //red||blue||green

四:栈方法(push(),pop()),队列方法(shirt(),push())

var colors = new Array();
var count = colors.push("red","blue","green");  //向后插入两项
alert(count);            //3
alert(colors);           //red,blue,green

var item = colors.pop();                //移除最后一项并返回
alert(item);            //green
alert(colors.length) //2

var item2 = colors.shift(); //移除第一项并返回
alert(item2); //red
alert(colors.length) //1

var count2 = colors.unshift("black","white");
alert(count2); //3
alert(colors); //black,white,blue

 

五:重排序方法(reverse(),sort())

var values = [0,1,5,10,15];
values.reverse();       //翻转数组
alert(values);          //15,10,5,1,0
values.sort();          //升序排列,先调用tostring()将数组项转换成字符串,然后比较字符串。
alert(values);          //0,1,10,15,5
values.sort(compare);   //加入比较函数
alert(values) //0,1,5,10,15
function compare(value1,value2){
return value1 - value1; //降序
}

六:操作方法(concat(),slice(),splice())

//concat():连接数组产生新的数组
var colors = ["red","blue","green"];
var colors2 = colors.concat("white",["black","yellow"]);
alert(colors);          //"red","blue","green"
alert(colors2);         //"red","blue","green","white","black","yellow"
//slice(start,[end])//基于当前数组产生一个新的数组
var colors = ["red","blue","green","black"];
var colors2 = colors.slice(1);
var colors3 = colors.slice(1,3);
alert(colors);//"red","blue","green","black"
alert(colors2);//"blue","green","black" 没有第二个参数则截取到数组末尾
alert(colors3);//"blue","green" 不包括end位置的项
//splice()删除,插入,替换
var colors = ["red","blue","green","black"];
var removed = colors.splice(0,1);//从下标为0开始,截取一项
alert(removed);//"red"
alert(colors);//"blue","green","black"
removed = colors.splice(1,0,"white","orange");//从下标为1开始,截取0个,并在当前位置插入"white","orange"。
alert(removed);//空,这里没有截取项
alert(colors);//"blue","white","orange","green","black"
removed = colors.splice(1,1,"red","purple");////从下标为1开始,截取1个,并在当前位置插入"white","orange"。
alert(removed);//""white
alert(colors);//"blue","red","purple","orange","green","black"

七:位置方法(indexOf(),lastIndexOf())

//indexOf(项,[起点的索引]),lastIndexOf(项,[起点的索引])。要找到的项必须严格相等
var numbers = [1,2,3,4,5,4,3,2,1]; //下标从0到1,不管是从先往后还是从后往前,数组下标不变
alert(numbers.indexOf(4));        //3
alert(numbers.lastIndexOf(4));    //5
alert(numbers.indexOf(4,4));      //5
alert(numbers.lastIndexOf(4,4));  //3

八:迭代方法(every(),some(),filter(),map()) 

 var numbers = [1,2,3,4,5,6];
 var everyResult = numbers.every(function(item,index,array){
 return (item > 2);          //如果所有item都满足条件,才会返回true
 });
 alert(everyResult);         //false
var someResult = numbers.some(function(item,index,array){ return(item>2); //如果有一个item满足条件,会返回true }); alert(someResult); //true
var filterResult = numbers.filter(function(item,index,array){ return (item > 2); //截取满足条件的item并返回 }); alert(filterResult); //3,4,5,6
var mapResult = numbers.map(function(item,index,array){ return (item * 2); //对所有item操作并返回 }); alert(mapResult);//2,4,6,8,10,12

九:归并方法(reduce(),reduceRight())

 var numbers = [1,2,3,4,5,6];
 var sum = numbers.reduce(function(prev,cur,index,array){
 return prev + cur;//返回的值会作为下一个prev继续操作,最终返回的是所有项相加的值.从左往右
 });
 alert(sum);//21
 var sum2 = numbers.reduceRight(function(prev,cur,index,array){
 return prev + cur;//返回的值会作为下一个prev继续操作,最终返回的是所有项相加的值.从右往左
 });
 alert(sum2);//21

  

 

  

var numbers = [1,2,3,4,5,4,3,2,1];//下标从0到1,不管是从先往后还是从后往前,数组下标不变
alert(numbers.indexOf(4));//3
alert(numbers.lastIndexOf(4));//5
alert(numbers.indexOf(4,4));//5
alert(numbers.lastIndexOf(4,4));//3
posted @ 2016-10-26 22:16  _林冲  阅读(2551)  评论(0编辑  收藏  举报