创建Array对象
复制代码 代码如下:
//one
var aValues=new Array();
//two
var aValues=new Array(20);
//three
var aColors=new Array();
aColors[0]="red";
aColors[1]="green";
aColors[2]="blue";
//four
var aColors=new Array("red","green","blue");
//five
var aColors=["red","green","blue"];
join && split
join:连接字符串
复制代码 代码如下:
var aColors=["red","green","blue"];
alert(aColors.join(","));//outputs "red,green,blue"
alert(aColors.join("-spring-"));//outputs "red-spring-green-spring-blue"
alert(aColors.join("]["));//outputs "red][green][blue"
split:分拆字符串
复制代码 代码如下:
var sColors="red,green,blue";
var aColors=sColors.split(",");//outputs ["red", "green", "blue"]
var redColors=aColors[0].split("");//outputs ["r", "e", "d"]
concat && slice
concat:追加数组
复制代码 代码如下:
var aColors=["red","green","blue"];
var aColors2=aColors.concat("yellow","purple");
alert(aColors);//outputs ["red", "green", "blue"]
alert(aColors2);//outputs ["red", "green", "blue", "yellow", "purple"]
slice:返回具有特定项的新数组
复制代码 代码如下:
var aColors=["red","green","blue","yellow","purple"];
var aColors2=aColors.slice(1);//outputs ["green","blue","yellow","purple"]
var aColors3=aColors.slice(1,4);//outputs ["green","blue","yellow"]
push && pop
跟栈一样,Array提供了push和pop方法,push方法用于在Array结尾添加一个或多个项,pop用于删除最后一个数组项,返回它作为函数值
复制代码 代码如下:
var stack=new Array;
stack.push("red");
stack.push("green");
stack.push("blue");
alert(stack);//outputs ["red","green","blue"]
var vItem=stack.pop();
alert(vItem);//outputs ["blue"]
alert(stack);//otputs ["red","green"]
shift && unshift
shift:删除数组中第一项,将其作为函数返回值,unshift:把一个项放在数组的第一个位置,然后把余下的项向下移动一个位置
复制代码 代码如下:
var aColors=["red","green","blue"];
var vItem=aColors.shift();
alert(aColors);//outputs ["green","blue"]
alert(vItem);//outputs ["red"]
aColors.unshift("black");
alert(aColors);//outputs ["black","green","blue"]
reverse && sort
reverse:颠倒数组项的顺序,sort:按数组项的值升序排列(首先要调用toString()方法,将所有值转换成字符串)
复制代码 代码如下:
var aColors=["blue","green","red"];
aColors.reverse();
alert(aColors);//outputs ["red","green","blue"]
aColors.sort();
alert(aColors);//outputs ["blue","green","red"]
注意:
复制代码 代码如下:
var aColors=[3,32,2,5];
aColors.sort();
alert(aColors);//outputs [2,3,32,5]
这是因为数字被转换成字符串,然后按字符代码进行比较的。
splice
splice:把数据项插入数组的中部
1、用作删除:只要声明两个参数,第一个参数为要删除的第一个项的位置,第二个参数为删除项的个数
复制代码 代码如下:
var aColors=["red","green","blue","yellow"];
aColors.splice(0,2);
alert(aColors);//outputs ["blue", "yellow"]
2、用作插入:声明三个或以上参数(第二个参数为0)就可以把数据插入指定位置,第一个参数为地始位置,第二个参数为0,第三个及以上参数为插入项
复制代码 代码如下:
var aColors=["red","green","blue","yellow"];
aColors.splice(2,0,"black","white");
alert(aColors);//outputs ["red","green","black","white","blue", "yellow"]
3、用作删除并插入:声明三个或以上参数(第二个参数为不0)就可以把数据插入指定位置,第一个参数为地始位置,第二个参数为要删除的项的个数,第三个及以上参数为插入项
复制代码 代码如下:
var aColors=["red","green","blue","yellow"];
aColors.splice(2,1,"black","white");
alert(aColors);//outputs ["red","green","black","white", "yellow"]
复制代码 代码如下:
//one
var aValues=new Array();
//two
var aValues=new Array(20);
//three
var aColors=new Array();
aColors[0]="red";
aColors[1]="green";
aColors[2]="blue";
//four
var aColors=new Array("red","green","blue");
//five
var aColors=["red","green","blue"];
join && split
join:连接字符串
复制代码 代码如下:
var aColors=["red","green","blue"];
alert(aColors.join(","));//outputs "red,green,blue"
alert(aColors.join("-spring-"));//outputs "red-spring-green-spring-blue"
alert(aColors.join("]["));//outputs "red][green][blue"
split:分拆字符串
复制代码 代码如下:
var sColors="red,green,blue";
var aColors=sColors.split(",");//outputs ["red", "green", "blue"]
var redColors=aColors[0].split("");//outputs ["r", "e", "d"]
concat && slice
concat:追加数组
复制代码 代码如下:
var aColors=["red","green","blue"];
var aColors2=aColors.concat("yellow","purple");
alert(aColors);//outputs ["red", "green", "blue"]
alert(aColors2);//outputs ["red", "green", "blue", "yellow", "purple"]
slice:返回具有特定项的新数组
复制代码 代码如下:
var aColors=["red","green","blue","yellow","purple"];
var aColors2=aColors.slice(1);//outputs ["green","blue","yellow","purple"]
var aColors3=aColors.slice(1,4);//outputs ["green","blue","yellow"]
push && pop
跟栈一样,Array提供了push和pop方法,push方法用于在Array结尾添加一个或多个项,pop用于删除最后一个数组项,返回它作为函数值
复制代码 代码如下:
var stack=new Array;
stack.push("red");
stack.push("green");
stack.push("blue");
alert(stack);//outputs ["red","green","blue"]
var vItem=stack.pop();
alert(vItem);//outputs ["blue"]
alert(stack);//otputs ["red","green"]
shift && unshift
shift:删除数组中第一项,将其作为函数返回值,unshift:把一个项放在数组的第一个位置,然后把余下的项向下移动一个位置
复制代码 代码如下:
var aColors=["red","green","blue"];
var vItem=aColors.shift();
alert(aColors);//outputs ["green","blue"]
alert(vItem);//outputs ["red"]
aColors.unshift("black");
alert(aColors);//outputs ["black","green","blue"]
reverse && sort
reverse:颠倒数组项的顺序,sort:按数组项的值升序排列(首先要调用toString()方法,将所有值转换成字符串)
复制代码 代码如下:
var aColors=["blue","green","red"];
aColors.reverse();
alert(aColors);//outputs ["red","green","blue"]
aColors.sort();
alert(aColors);//outputs ["blue","green","red"]
注意:
复制代码 代码如下:
var aColors=[3,32,2,5];
aColors.sort();
alert(aColors);//outputs [2,3,32,5]
这是因为数字被转换成字符串,然后按字符代码进行比较的。
splice
splice:把数据项插入数组的中部
1、用作删除:只要声明两个参数,第一个参数为要删除的第一个项的位置,第二个参数为删除项的个数
复制代码 代码如下:
var aColors=["red","green","blue","yellow"];
aColors.splice(0,2);
alert(aColors);//outputs ["blue", "yellow"]
2、用作插入:声明三个或以上参数(第二个参数为0)就可以把数据插入指定位置,第一个参数为地始位置,第二个参数为0,第三个及以上参数为插入项
复制代码 代码如下:
var aColors=["red","green","blue","yellow"];
aColors.splice(2,0,"black","white");
alert(aColors);//outputs ["red","green","black","white","blue", "yellow"]
3、用作删除并插入:声明三个或以上参数(第二个参数为不0)就可以把数据插入指定位置,第一个参数为地始位置,第二个参数为要删除的项的个数,第三个及以上参数为插入项
复制代码 代码如下:
var aColors=["red","green","blue","yellow"];
aColors.splice(2,1,"black","white");
alert(aColors);//outputs ["red","green","black","white", "yellow"]
标签:
本地对象,Array
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“JavaScript高级程序设计 读书笔记之九 本地对象Array”评论...
更新动态
2024年11月15日
2024年11月15日
- 谭咏麟《20世纪中华歌坛名人百集珍藏版》[WAV+CUE][1G]
- 炉石传说40轮盘术最新卡组代码在哪找 标准40轮盘术卡组代码分享
- 炉石传说亲王贼怎么玩 2024亲王贼最新卡组代码分享
- 炉石传说30.6.2补丁后有什么卡组 30.6.2最强卡组最新推荐
- 模拟之声慢刻CD《蔡琴名曲回顾遇听》[原抓WAV+CUE]
- BruceLiu-WAVES(MusicbySatie)(2024)2CD[24Bit-96kHz]FLAC
- KonstantinKrimmel-MythosSchubertLoewe(2024)[24Bit-96kHz]FLAC
- 2024雷蛇高校挑战赛 嘤式分解助力收官之战
- 海信发布110吋世俱杯官方定制AI电视 引领智能观赛
- 海信发布27英寸显示器大圣G5 Pro:采用自研超解析芯片、友达原厂模组
- 蔡琴《机遇》1:1母盘直刻日本头版[WAV分轨][1.1G]
- 陈百强《与你几分钟的约会》XRCD+SHMCD限量编号版[低速原抓WAV+CUE][994M]
- 陈洁丽《监听王NO.1 》示范级发烧天碟[WAV+分轨][1.1G]
- 单色凌.2014-小岁月太着急【海蝶】【WAV+CUE】
- 陈淑桦.1988-抱紧我HOLD.ME.NOW【EMI百代】【WAV+CUE】