码迷,mamicode.com
首页 > 编程语言 > 详细

数组方法

时间:2016-03-30 19:33:06      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:

json数据格式

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>

<script>
var json = { name : ‘leo‘, age : 32 };
// alert( json.name );

var arrUrl = [ ‘img/1.png‘, ‘img/2.png‘, ‘img/3.png‘, ‘img/4.png‘ ];
var arrText = [ ‘小宠物‘, ‘图片二‘, ‘图片三‘, ‘面具‘ ];

var imgData = {
url : [ ‘img/1.png‘, ‘img/2.png‘, ‘img/3.png‘, ‘img/4.png‘ ],
text : [ ‘小宠物‘, ‘图片二‘, ‘图片三‘, ‘面具‘ ]
};
// alert( imgData.url[2] );

// var json2 = { name : ‘miaov‘ };
var json2 = { ‘name‘ : ‘miaov‘ };

// alert( json2.name );
// alert( json2[‘name‘] );
json2.name = ‘妙味‘;
json2[‘name‘] = ‘miaov‘;
// alert(json2.name);

var json3 = { abc : 123, xyz : ‘‘ };
// { [], [], [] } [ {}, {}, {} ]
var arr = [ { ‘name‘ : ‘TM‘, ‘age‘ : 23 }, { ‘name‘ : ‘leo‘, ‘age‘ : 32 } ];
// alert( arr[0].name + ‘今年有‘ + arr[1][‘age‘] );


var json4 = { ‘name‘ : ‘miaov‘, ‘age‘ : 3, ‘fun‘ : ‘前端开发‘ };

for ( var attr in json4 ) {
// alert( attr );
// alert( json4[attr] );
}

var json5 = {
‘url‘ : [ ‘img/1.png‘, ‘img/2.png‘, ‘img/3.png‘, ‘img/4.png‘ ],
‘text‘ : [ ‘小宠物‘, ‘图片二‘, ‘图片三‘, ‘面具‘ ]
};

// var arr = [ {}, {}, {} ];

for ( var attr in json5 ) {
for ( var i=0; i < json5[attr].length; i++ ) {
alert( json5[attr][i] );
}
}


</script>

</head>

<body>
</body>
</html>

 

for-in  遍历对象属性

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
</head>

<body>

<script>
var str = ‘‘;
var num = 0;
for ( var attr in document ) {
str += num + ‘. ‘ + attr + ‘:‘ +document[attr] + ‘<br />‘;
num ++;
}
document.body.innerHTML = str;
</script>

</body>
</html>

 

for循环和for-in的使用

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>

<script>
var json = {
‘url‘ : [ ‘img/1.png‘, ‘img/2.png‘, ‘img/3.png‘, ‘img/4.png‘ ],
‘text‘ : [ ‘小宠物‘, ‘图片二‘, ‘图片三‘, ‘面具‘ ]
};

var arr = [ ‘a‘, ‘b‘, ‘c‘ ];

for ( var i in arr ) {
alert( arr[i] );
}

</script>

</head>

<body>
</body>
</html>

数组的添加方法

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>

<script>

var arr = [ 1,2,3 ];

// alert( arr.push( ‘abc‘ ) );

// alert( arr );

alert( arr.unshift( 0 ) ); // IE 6 7 不支持 unshift 返回值

// alert( arr );

</script>

</head>

<body>
</body>
</html>

数组的删除方法

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>

<script>

var arr = [ ‘TM‘, ‘钟毅‘, ‘张森‘, ‘杜鹏‘, ‘Leo‘ ];
// alert( arr.pop() );
// alert( arr );
alert( arr.shift() );
// arr.shift();
alert( arr );


</script>

</head>

<body>
</body>
</html>

数组排队

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>

<script>

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

// arr1.reverse();

// alert( arr1 );

var str = ‘abcdef‘;
// alert(str.split(‘‘).reverse().join(‘‘));

</script>

</head>

<body>
</body>
</html>

 

splice使用(功能包括:删除、替换、添加)

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>

<script>

var arr = [ ‘TM‘, ‘钟毅‘, ‘张森‘, ‘杜鹏‘, ‘Leo‘ ];
// 删除、替换、添加
alert( arr.splice( 0 , 0, ‘aaaaaa‘ ) );

// arr.splice( 0, 2, ‘莫涛 or 钟毅‘ );

// alert( arr.splice( 1, 0, ‘钟毅媳妇儿~‘, ‘钟毅媳妇们~‘ ) );

alert( arr );


</script>

</head>

<body>
</body>
</html>

数组去重

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>

<script>
var arr = [ 1,2,2,4,2 ];

for ( var i=0; i<arr.length; i++ ) {
for ( var j=i+1; j<arr.length; j++ ) {
if ( arr[i] == arr[j] ) {
arr.splice( j, 1 );
j--;
}
}
}
alert( arr );

</script>

</head>

<body>
</body>
</html>

sort排序使用

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>

<script>
var arr = [ ‘c‘, ‘d‘, ‘a‘, ‘e‘ ];
// arr.sort();
// alert( arr );

var arr2 = [ 4,3,5,5,76,2,0,8 ];
// arr2.sort();
// alert( arr2 );

arr2.sort(function ( a, b ) {
return a - b;
});

// alert( arr2 );

var arrWidth = [ ‘345px‘, ‘23px‘, ‘10px‘, ‘1000px‘ ];

arrWidth.sort(function ( a, b ) {
return parseInt(a) - parseInt(b);
});

alert( arrWidth );

</script>

</head>

<body>
</body>
</html>

随机排序

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>

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

arr.sort(function ( a, b ) {
return Math.random() - 0.5;
});

alert( arr );

// alert( Math.random() );

</script>

</head>

<body>
</body>
</html>

随机函数

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>

<script>
// alert( Math.round(3.4) );    //四舍五入函数
// 0~1 : Math.round(Math.random());
// 0~10
// alert( Math.round(Math.random()*10) );

// 5~10
// alert( Math.round( Math.random()*5 + 5 ) );

// 10~20
// alert( Math.round( Math.random()*10 + 10 ) );

// 20~100
// alert( Math.round( Math.random()*80 + 20 ) );

// x ~ y
var x = 3;
var y = 49;
// alert( Math.round( Math.random()*(y-x) + x ) );

// 0~x
// alert( Math.round( Math.random()*x) );

// 1~x
alert( Math.ceil( Math.random()*x) );


// 课上小练习:
// 随机产生 100 个从 0 ~ 1000 之间不重复的整数
// var str = ‘aaasdlfjhasdlkfs‘;
// indexOf(‘s‘) —— 为数组编写该方法:indexOf(‘img/1.jpg‘)

</script>

</head>

<body>
</body>
</html>

concat方法使用

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>

<script>

var arr1 = [ 1,2,3 ];
var arr2 = [ 4,5,6 ];
var arr3 = [ 7,8,9 ];

alert( arr1.concat( arr2, arr3 ) );

</script>

</head>

<body>
</body>
</html>

reverse方法使用

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>

<script>

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

// arr1.reverse();

// alert( arr1 );

var str = ‘abcdef‘;
// alert(str.split(‘‘).reverse().join(‘‘));

</script>

</head>

<body>
</body>
</html>

数组方法

标签:

原文地址:http://www.cnblogs.com/webdjk/p/5338234.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!