博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js创建及操作数组
阅读量:7067 次
发布时间:2019-06-28

本文共 4355 字,大约阅读时间需要 14 分钟。

一:创建数组

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;   //数组长度缩短为2colors[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);            //3alert(colors);           //red,blue,greenvar 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,0values.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));        //3alert(numbers.lastIndexOf(4));    //5alert(numbers.indexOf(4,4));      //5alert(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

转载于:https://www.cnblogs.com/momozjm/p/6002134.html

你可能感兴趣的文章
【iOS开发-47】怎样下载iOS 7.1 Simulator 以及iOS 8离线的Documentation这些文件?
查看>>
多种方式求阶乘
查看>>
页面中引入mui 地址选择,点击页面中其他input时页面回到顶部
查看>>
js团购倒计时
查看>>
9.19 数组 冒泡排序和二分法
查看>>
妈了个蛋,写了个炒鸡垃圾的脚本,也是醉了
查看>>
图论1——基础
查看>>
蒙哥玛利模幂算法
查看>>
龙威零式_团队项目例会记录_14
查看>>
YAFFS2文件系统分析(转)
查看>>
获取GET/POST提交的数据,并处理中文问题
查看>>
jdbc 获取connection 对象的三种方式
查看>>
jsp标签+jstl
查看>>
第二阶段个人总结09
查看>>
FATAL ERROR: Could not find ./bin/my_print_defaults的解决办法
查看>>
文摘《十一》
查看>>
jquery 笔记。。。——》摘自武方博
查看>>
一个夭折,
查看>>
C#开发微信门户及应用(1)--开始使用微信接口(转)
查看>>
Kali-linux使用社会工程学工具包(SET)
查看>>