数组

(1)数组length属性

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>数组</title>
</head>
<script type="text/javascript">
var arr=[1,2,3,4,5,6];
alert(arr.length);
</script>
<body>

</body>
</html>

设置数组的具体几个

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>数组</title>
</head>
<script type="text/javascript">
var arr=[1,2,3,4,5,6];
arr.length=3;
alert(arr);
</script>
<body>

</body>
</html>

数组既可以获取,还可以设置

(3)快速清空数组

(4)数组的方法

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>数组</title>
</head>
<script type="text/javascript">
var arr=[1,2,3];
/*为数组结尾添加元素*/
arr.push(4);
alert(arr);
/*为数组结尾删除元素*/
arr.pop();
alert(arr);
/*在数组头部添加元素*/
arr.shift();
alert(arr);
/*在数组头部删除元素*/
arr.unshift();
alert(arr);
</script>
<body>

</body>
</html>

不是在中间部分的添加和删除元素:先删除,再添加

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>数组</title>
</head>
<script type="text/javascript">
var arr=[1,2,3,4,5,6];
/*插入splice(起点,长度,元素...)*/
arr.splice(2,0,'a','b','c');
alert(arr);
</script>
<body>

</body>
</html>

数组的操作:连接

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>数组</title>
</head>
<script type="text/javascript">
var a=[1,2,3];
var b=[4,5,6];
alert(a.concat(b));

</script>
<body>

</body>
</html>

数组连接符:join

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>数组</title>
</head>
<script type="text/javascript">
var arr=[1,2,3,4];
alert(arr.join('- -p'));
</script>
<body>

</body>
</html>

数组的排序:sort

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>数组</title>
</head>
<script type="text/javascript">
var arr=['float','width','alpha','zoom','left'];
arr.sort();
alert(arr);
</script>
<body>

</body>
</html>

数组数字排序:sort加函数(比较函数

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>数组</title>
</head>
<script type="text/javascript">
var arr=[12,8,99,19,112];
arr.sort(function(n1,n2){
    return n1-n2;
});
alert(arr);
</script>
<body>

</body>
</html>

 

 

posted on 2015-06-09 13:13  zhangjingyun  阅读(148)  评论(0编辑  收藏  举报