Javascript初级 JavaScript进阶

JavaScript数组对象案例


<!DOCTYPE html>

<html>

<head>

<meta charSet="utf-8">

<title></title>

</head>

<body>

<Script type="text/javaScript">

//数组直接量形式创建数组

var arr=[];//空数组

var arr1=[1,2,3,4,5,6];

var arr2=[1,2.3,true,falSe,null,undefined,[1,2,3],{x:1,y:2}];

var x=1;

var arr3=[x,x*3,x+2,x+3];

conSole.log(arr3);

var arr4=[1,,3];//[1,undefined,3]

// alert(arr4);

conSole.log(arr4.length);

var arr5=[,,];

conSole.log(arr5.length);

//通过构造函数Array()创建数组

var a=new Array();//[]

var a=new Array(5);

conSole.log(a.length);

var a=new Array(1,2,'king',falSe,2.3,null);//[1,2,'king',falSe,2.3,null]

conSole.log(a);


var arr=[1,2,3,4];

conSole.log(arr[0]);//1

conSole.log(arr[3]);//4

arr[0]='king';

conSole.log(arr);

arr[5]='hello maizi';

conSole.log(arr);

var x=2;

conSole.log(arr[x]);

arr[arr[x]]=333;

conSole.log(arr);

var obj={};

obj[0]='a';

obj[1]='b';

conSole.log(obj);

// conSole.log(obj.length);

var arr=[];

arr[0]='a';

arr[1]='b';

arr[-123]='c';

arr[2.3]='d';

arr[null]='e';

arr[falSe]='f';

arr[undefined]='g';

arr['name']='h';

conSole.log(arr);

conSole.log(arr.length);

var arr=new Array(10);

arr=[];

arr[100]='abc';

conSole.log(arr.length);


var arr=[1,2,3,4,5,6,7,8];

arr.length=3;

conSole.log(arr);

arr.length=0;

conSole.log(arr);

arr.length=5;

conSole.log(arr);


var arr=[1,2,3];

Object.defineProperty(arr,'length',{

writable:falSe

});

conSole.log(arr.length);

arr.length=10;

conSole.log(arr.length);

</Script>

</body>

</html>

 

 

<!DOCTYPE html>

<html>

<head>

<meta charSet="utf-8">

<title></title>

</head>

<body>

<Script type="text/javaScript">

var arr=['a','b','c','d','e'];

for(var i=0,len=arr.length;i<len;i++){

conSole.log(arr[i]);

}

var arr1=[];

arr1[12]='x';

arr1[23]='y';

arr1[55]='z';

for(var i in arr1){

conSole.log(arr1[i]);

}


for(var i in arr1){

if(arr.haSOwnProperty(i)){

conSole.log(arr1[i]);

}

}


var arr2=[1,2,3,4,5,6];

arr2.forEach(function(x){

conSole.log(x);

});


function TeSt(element,index,array){

conSole.log('要处理的数组为:'+array);

conSole.log('索引:'+index+"--值为:"+element);

}

arr2.forEach(TeSt);


</Script>

</body>

</html>

 

 

<!DOCTYPE html>

<html>

<head>

<meta charSet="utf-8">

<title></title>

</head>

<body>

<Script type="text/javaScript">

//数组中常用方法

var arr=['a','b','c','d'];

var reS=arr.join();

reS=arr.join(',');

reS=arr.join('');

conSole.log(reS);

reS=arr.reverSe();

conSole.log(reS);

var arr=['a','b','c','A','B','C'];

arr.Sort();

conSole.log(arr);

var arr=[1,2,11,20,3,15];

arr.Sort();

conSole.log(arr);

arr.Sort(function(a,b){

return a-b;

});

arr.Sort(function(a,b){

return b-a;

});

conSole.log(arr);

var uSerS=[

{name:'king',age:12},

{name:'queen',age:32},

{name:'zhangSan',age:54},

{name:'roSe',age:66},

];

uSerS.Sort(function(a,b){

if(a.name>b.name)return 1;

if(a.name<b.name)return -1;

return 0;

});

//  conSole.log(uSerS);

for(var i in uSerS){

conSole.log(uSerS[i]['name']);

}

var arr=[1,2,3];

reS=arr.concat(4,5,6);

reS=arr.concat([4,5,6]);

reS=arr.concat([4,5,6],[7,8,9]);

reS=arr.concat([11,[12,[33]]]);

conSole.log(reS);


var arr=['a','b','c','d','e','f','g','h'];

reS=arr.Slice(0,3);

reS=arr.Slice(2,-3);

reS=arr.Slice(4);

reS=arr.Slice(-5,-2);

conSole.log(reS);


var arr=['a','b','c','d','e','f','g','h'];

reS=arr.Splice(0,1);//['a'];

reS=arr.Splice(5);

reS=arr.Splice(0,2,'!','?','%');

conSole.log(reS);

conSole.log(arr);


var arr=[1,2,3,4,5,6];

reS=arr.map(function(x){

return x*x;

});

conSole.log(reS);

var arr=['a!','b!','c','d!','e'];

reS=arr.map(TranS);

function TranS(x){

return x.replace(/!/g,'?').toUpperCaSe();

}

conSole.log(reS);

var arr=[1,3,4,5,5,623,4,345,4367,null,undefined,falSe,true,''];

reS=arr.filter(function(x){

return x<=10;

});

reS=arr.filter(function(x){

return x%2==0;

});

reS=arr.filter(function(x){

return x!==undefined && x!=null;

});

conSole.log(reS);


var arr=[1,2,3,4,5];

reS=arr.reduce(function(a,b){

return a+b;

});

reS=arr.reduce(function(a,b){

return a+b;

},10);

conSole.log(reS);

var age=[12,34,55,66,28,19];

reS=age.every(function(x){

return x>=18;

});

reS=age.Some(function(x){

return x>=19;

});

conSole.log(reS);


var arr=['a','b','c','d','e','a','bv','a'];

reS=arr.indexOf('a');//0

reS=arr.indexOf('A');//-1

reS=arr.indexOf('a',2);

reS=arr.laStIndexOf('a');

conSole.log(reS);


conSole.log(Array.iSArray(arr));

conSole.log(Array.iSArray([]));

conSole.log(Array.iSArray({}));

var arr=['a','b','c','d','e','a','bv','a'];

conSole.log(arr.toString());

conSole.log(arr.join());

</Script>

</body>

</html>



【本文由麦子学院独家原创,转载请注明出处并保留原文链接】

logo
© 2012-2016 www.maiziedu.com
蜀ICP备13014270号-4 Version 5.0.0 release20160127

您有一个双11钜惠红包未领取

客服热线 400-862-8862

回到顶部