《JavaScript高级程序设计》读书笔记之九:本地对象Array

来源:未知 责任编辑:责任编辑 发表时间:2014-05-26 10:59 点击:

创建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:把一个项放在数组的第一个位置,然后把余下的项向下移动一个位置www.2cto.com
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"];

发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码:点击我更换图片
最新评论 更多>>

推荐热点

  • Gb2312转utf-8编码的方法(vbs+js)
  • 如何使用Ajax技术开发Web应用程序(1)
  • js跳转路径问题
  • JavaScript模仿桌面窗口
  • 用js检测两个线段是否相交
  • 我知道的JavaScript -- 设计模式(桥接)应用之 – 验证器
  • 运用JavaScript构建你的第一个Metro式应用程序(on Windows
  • 我是如何去了解jquery的(六),案例之幻灯片轮换
  • Jquery封装幻灯片效果
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1