js Array

来源:互联网 发布:mac命令行终端工具 编辑:程序博客网 时间:2024/04/29 08:32

js数组最多可包含4294967295个项(JavaScript高级程序设计第二版),超过项数上限值会发生异常;创建一个初始大小与这个上限值接近的数组,可能导致运行时间超长的脚本错误。

创建数组基本方式有两种。

第一种创建数组方式:使用Array构造函数。

new Array()创建一个空数组;

new  Array(3)创建一个包含3项的数组,每项的初始值都为undefined;

new Array(("red","green")创建一个定项数数组,并为每项赋值;

可以省略new操作符;

var colors = new Array();//new一个空数组colors.push("red", "gree", "blue");//document.write(colors + "<br>");//数组长度为0var colors1 = new Array(3);document.write(colors1 + "<br>");//,,;每一项的初始值都为undefinedcolors1.push("purple");document.write(colors1 + "<br>");//,,,purplevar colors2 = new Array("red","green");colors2.push("blue");document.write(colors2 + "<br>");//red,green,bluevar colors3 = Array(3);document.write(colors3 + "<br>");//,,var colors4 = Array("red","green");document.write(colors4 + "<br>");//red,green

第二种创建数组方式:使用数组字面量表示法;

使用数组字面量表示法,不会去调用Array构造函数(Firefox除外)

数组最后一项不要添加英文逗号(IE8-版本与其他浏览器展示的数组长度不同)

length属性可以操作数组(增减了数组的项数)

var colors = [];//new一个空数组colors.push("red", "gree", "blue");//document.write(colors + "<br>");//数组长度为0var colors1 = ["red","green"];colors1.push("blue");document.write(colors1 + "<br>");//red,green,bluecolors1.length=2;//上面length==3document.write(colors1 + "<br>");//red,greencolors1.length=4;document.write(colors1 + "<br>");//red,green,,colors1[6] = "black";//在索引6(第七项)加上一项,实际长度只有4,中间多了2项undefined项document.write(colors1 + "<br>");var num = [1,2,];document.write(num.length);//IE8-版本数组长度为3,IE9+和Firefox、Chrome、Safari都是2
数组继承的toLocaleString()、toString()、valueOf()在默认情况下以逗号分隔的字符串形式返回数组项;
join()用不同的分隔符来构建数组字符串
var colors = ["red","green","blue"];document.write(colors + "<br>");//red,green,blue后台调用了toString()方法document.write(colors.toString() + "<br>");//red,green,bluedocument.write(colors.toLocaleString() + "<br>");//red,green,bluedocument.write(colors.valueOf() + "<br>");//red,green,blue//join分隔符分割字符串document.write(colors.join() + "<br>");//red,green,bluedocument.write(colors.join(",") + "<br>");//red,green,bluedocument.write(colors.join("|") + "<br>");//red|green|blue
栈方法(后进先出:push和pop组合)
push()方法可以接收任何数量的参数,把它们逐个添加到数组末尾,并返回数组修改后的长度;
pop()返回数组最后一项,数组会移除掉最后一项;
var colors = ["red","green"];document.write(colors.push("blue") + "<br>");//3document.write(colors + "<br>");//red,green,bluedocument.write(colors.pop()+ "<br>");//bluedocument.write(colors + "<br>");//red,green
队列方法(先进先出:push和shift组合)
unshift()在数组开头添加数组元素,可以添加任意个
var colors = ["blue"];document.write(colors.unshift("red", "green") + "<br>");//3document.write(colors);//red,green,blue
concat基于当前数组创建一个新数组
var colors = ["red"];document.write(colors.concat("green", "blue") + "<br>");//red,green,blue
slice(startIndex, endIndex)与string的slice用法是一样的;
索引为负值时则从数组第-1个元素从右向左数,确定各自的正数值索引;
当startIndex大于endIndex则返回undefined
var colors = ["red","green", "blue" ,"yellow", "white", "black"];document.write(colors.slice(1) + "<br>");//green,blue,yellow,white,blackdocument.write(colors.slice(2,4) + "<br>");//blue,yellowdocument.write(colors.slice(4,2) + "<br>");//document.write(colors.slice(-2,-4) + "<br>");//document.write(colors.slice(-4,-2) + "<br>");//blue,yellow
splice(起始位置索引, 要删除的项数, 后面为要插入的任意个项)
splice()可以新增、修改、删除数组元素,返回的结果为移除了的元素的数组;
原数组实现新增、修改、删除
var colors = ["red", "black"];document.write(colors.splice(1,0,"green", "blue") + "<br>");//返回空数组,(新增)document.write(colors + "<br>");//red,green,blue,blackdocument.write(colors.splice(1,2) + "<br>");//green,blue,返回删除的数组(删除)document.write(colors + "<br>");//red,blackdocument.write(colors.splice(1,1,"green", "blue") + "<br>");//black,返回删除的数组document.write(colors);//red,green,blue
arrayObj.reverse()反转数组排列顺序
reverse()返回的结果和reverse()后的原数组,都为最初的反序;
var num = [1,3,7,5,9];document.write(num + "<br>");//1,3,7,5,9document.write(num.reverse() + "<br>");//9,5,7,3,1document.write(num + "<br>");//9,5,7,3,1var str = ['one','three','four','two'];document.write(str + "<br>");//one,three,four,twodocument.write(str.reverse() + "<br>");//two,four,three,onedocument.write(str + "<br>");//two,four,three,one
sort()按升序排列数组项
sort()返回的数组和sort()后原数组的顺序都为原数组的升序结果;
sort()方法会调用每个数组项的toString(),比较时都是用的字符串比较,数值就没法正确排序;
通过调用compare(value1, value2)自定义函数为数值数组排序(如果想数组从大到小排序,只须改变compare中返回值的1和-1)
var num1 = [1,3,7,5,9];document.write(num1.sort() + "<br>");//1,3,5,7,9document.write(num1 + "<br>");//1,3,5,7,9var num2 = [1,3,7,5,9,15];//按正确的排序方式num2中的15本应该排在最后document.write(num2.sort() + "<br>");//1,15,3,5,7,9document.write(num2 + "<br>");//1,15,3,5,7,9var num3 = [1,3,7,5,9,15];document.write(num3.sort(compare) + "<br>");//1,3,7,5,9,15document.write(num3 + "<br>");//1,3,7,5,9,15function compare(value1, value2){  if(value1 < value2){    return -1;  }else if(value1 > value2){    return -1;  }else{    return 1;  }}








0 0
原创粉丝点击