Heim  >  Artikel  >  Web-Frontend  >  javascript学习笔记(五) Array 数组类型介绍_基础知识

javascript学习笔记(五) Array 数组类型介绍_基础知识

WBOY
WBOYOriginal
2016-05-16 17:52:311309Durchsuche

数组的创建
第一种:

复制代码 代码如下:

var colors = new Array();
var colors = new Array(20);//创建包含20项的数组
var colors = new Array("Greg");//创建包含1项,即字符串"Greg"的数组
var colors = new Array("red","blue","green"); //创建包含3项

第二种:
复制代码 代码如下:

var colors = ["red","blue","green"];
var colors = [];//创建一个空数组

注意:数组的索引是从0开始的

1. length属性
length属性中保存数组的项数,如:
复制代码 代码如下:

var colors = ["red","blue","green"];
alert(colors.length); //3

length属性不是只读的,可以利用length属性在数组的末尾移除项,或者添加新的项,如:
复制代码 代码如下:

var colors = ["red","blue","green"];
colors.length = 2;
alert(colors); //red,blue
colors[colors.length] = "black";
alert(colors); //red,blue,black

2.join()方法,连接数组中的项
复制代码 代码如下:

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

3.数组的栈方法:push()和pop()
push()方法 可以接受任意数量的参数把它们逐个添加的数组的末尾,并返回修改后数组的长度
pop()方法 从数组末尾移除最后一项,减少数组的length值,返回移除的项
复制代码 代码如下:

var colors = new Arrary(); //创建一个数组
var count = colors.push("red","green"); //推入两项到数组末尾
alert(count); //2
count = colors.push("black"); //推入一项到数组末尾
alert(count); //3
var item = colors.pop(); //移除最后一项并返回该值
alert(item); //"black"
alert(count); //2

4.数组的队列方法:push()和shift()、unshift()
push()方法同上
shift()方法 移除数组中的第一项并返回该项,数组长度减1
unshift()方法 在数组前端添加任意项,并返回新数组的长度
复制代码 代码如下:

var colors = new Arrary(); //创建一个数组
var count = colors.push("red","green"); //推入两项到数组末尾
alert(count); //2
count = colors.push("black"); //推入一项到数组末尾
alert(count); //3
var item = colors.shift(); //移除第一项并返回该值
alert(item); //"red"
alert(colors); //green,black
count = colors.unshift("blue"); //推入一项到数组前端
alert(count); //3
alert(colors); //blue,green,black

5.重排序方法:reverse()和sort()
reverse()方法 反转数组项的顺序
sort()方法 默认按字符串大小升序排列数组项,可以接受一个比较大小的函数作为参数
复制代码 代码如下:

var values = [1,2,3,4,5];
values.reverse();
alert(values); //5,4,3,2,1

复制代码 代码如下:

//升序排序函数
function compare(value1,value2) {
if (value1 return -1; //降序改为1
} else if (value1 > value2) {
return 1; //降序改为-1
} else {
return 0;
}
}

复制代码 代码如下:

//数组升序排列
var values = [0,1,5,15,20,10];
values.sort(compare);
alert(values);//0,1,5,10,15,20

复制代码 代码如下:

//对于数值型可以用这个函数,升序
function compare(value1,value2) {
return value2 - value1;
}

6.数组的一些方法:concat()方法、slice()方法和splice()方法
concat()方法 将参数添加到原数组末尾,返回新的数组,原数组不变
slice()方法 返回数组中的项,一个参数时返回指定位置到数组末尾所有的项,两个参数时返回起始位置和结束位置之间的项(不包括结束位置),原数组不变
splice()方法 向数组中插入,删除,或替换数组中的项,返回删除的项(没有删除时返回空数组),原数组改变
复制代码 代码如下:

//concat()方法
var colors = ["red","green","blue"];
var colors2 = colors.concat("yellow",["black","brown"]);
alert(colors); //red,green,blue
alert(colors2); //red,green,blue,yellow,black,brown

复制代码 代码如下:

//slice()方法
var colors = ["red","green","blue","yellow","black"];
var colors2 = colors.slice(1); //一个参数时返回指定位置到数组末尾所有的项
var colors3 = colors.slice(1,4); //两个参数时返回起始位置和结束位置之间的项(不包括结束位置)
alert(colors2); //green,blue,yellow,black
alert(colors3); //green,,blue,yellow

复制代码 代码如下:

//splice()方法
//插入项,插入时指定3个参数:起始位置、0(要删除的项)、要插入的项
var colors = ["red","green","blue"];
var inserted = colors.splice(1,0,"yellow","orange"); //从位置1开始插入两项
alert(colors); //red,yellow,orange,green,blue
alert(inserted); //空数组

//替换项,删除时指定3个参数:起始位置、要删除的项、要插入的任意项
var colors = ["red","green","blue"];
var replaced = colors.splice(1,1,"black","brown"); //删除一项,插入两项
alert(colors); //red,black,browm,blue
alert(replaced); //green
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn