js-Array数组

js-Array数组_第1张图片

一、创建数组的两种方式

1.使用Array构造函数

var colors = new Array();
var colors = new Array(20);
var colors = new Array("red","blue","green");

2.使用数组字面量表示法

var names = [];
var colors = ["red","blue","green"];
var values = [1,2,];   //!IE8及之前版本中,values会成为一个包含3个项(1,2,undefined)的数组,其它(1,2);
var options = [,,,,];   //同上

二、读取和设置数组值

var colors = ["red","blue","green"];
alert(colors[0]);      //显示第一项
colors[2] = "black";   //修改第三项
colors[3] = "brown";   //增加第四项

三、其他

1.length

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

var colors = ["red","blue","green"];
colors.length = 2;   //长度修改为2,则最后一项被删除
alert(colors[2]);    //结果:undefine 原因:长度为2,第三项被删

var colors = ["red","blue","green"];
colors.length = 4;   //长度修改为4,则新添加了一项
alert(colors[3]);    //结果:undefine  原因:长度为4,第四项未赋值

var colors = ["red","blue","green"];
colors[colors.length] = "black";  //添加新项目

2.Array.isArray()

if(Array.isArray(value)){
    //对数组执行操作
}

 

转载于:https://www.cnblogs.com/L-xmin/p/11528842.html

你可能感兴趣的:(js-Array数组)