var myArray = new Array();
var min_row = Math.min.apply(Math,myArray);
myArray.shift(); //去掉第一个数
一、创建数组
<script language="JavaScript" type="text/javascript">
<!--
var myArray = new Array(7);
myArray[0] = "Sunday";
myArray[1] = "Monday";
myArray[2] = "Tuesday";
myArray[3] = "Wednesday";
myArray[4] = "Thursday";
myArray[5] = "Friday";
myArray[6] = "Saturday";
document.write("<h3>数组中的元素有:"+myArray);
//-->
</script>
二、创建数组
<script language="JavaScript" type="text/javascript">
<!--
var myArray = new Array("星期日","星期一",
"星期二","星期三",
"星期四","星期五",
"星期六");
document.write("<h3>数组中的元素有:"+myArray);
//-->
</script>
三、创建数组对象
<script language="JavaScript" type="text/javascript">
<!--
var sString = new String("sunday");
var myArray = new Array("星期日",1,false,sString);
document.write("<h3>数组中的元素有:"+myArray);
//-->
</script>
四、数组的特殊函数用法(slice和join)
(1--slice用法)
<script language="JavaScript" type="text/javascript">
<!--
var myArray = new Array(7);
myArray[0] = "Sunday";
myArray[1] = "Monday";
myArray[2] = "Tuesday";
myArray[3] = "Wednesday";
myArray[4] = "Thursday";
myArray[5] = "Friday";
myArray[6] = "Saturday";
var newArray1 = myArray.slice(1,4);
document.write("<h3>新创建的第一个数组为:"+newArray1);
var newArray2 = myArray.slice(5);
document.write("<h3>新创建的第二个数组为:"+newArray2);
document.write("<h3>原数组为:"+myArray);
//-->
</script>
结果是:
新创建的第一个数组为:Monday,Tuesday,Wednesday
新创建的第二个数组为:Friday,Saturday
原数组为:Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday
(2--join用法)
<script language="JavaScript" type="text/javascript">
<!--
var myArray = new Array(7);
myArray[0] = "Sunday";
myArray[1] = "Monday";
myArray[2] = "Tuesday";
myArray[3] = "Wednesday";
myArray[4] = "Thursday";
myArray[5] = "Friday";
myArray[6] = "Saturday";
var sString = myArray.join("^-^");
document.write("<h3>数组中的数组元素内容为:"+sString);
//-->
</script>
结果为:
数组中的数组元素内容为:Sunday^-^Monday^-^Tuesday^-^Wednesday^-^Thursday^-^Friday^-^Saturday
五、数组排序:
(1)默认的排序:
<script language="JavaScript" type="text/javascript">
<!--
var myArray = new Array(7);
myArray[0] = "Sunday";
myArray[1] = "Monday";
myArray[2] = "Tuesday";
myArray[3] = "Wednesday";
myArray[4] = "Thursday";
myArray[5] = "Friday";
myArray[6] = "Saturday";
document.write("<h3>原数组为:"+myArray);
var newArray = myArray.sort();
document.write("<h3>新数组为:"+newArray);
//-->
</script>
结果:
原数组为:Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday
新数组为:Friday,Monday,Saturday,Sunday,Thursday,Tuesday,Wednesday
(2)指定比较规则的排序
<html>
<head>
<title>对数组元素的操作</title>
<script language="JavaScript" type="text/javascript">
<!--
function myFunction(element1, element2)
{
if (element1 < element2){
return -1;
}else if(element1==element2){
return 0;
}else {
return 1;
}
}
//-->
</script>
</head>
<body>
<h1>对数组元素的操作</h1>
<script language="JavaScript" type="text/javascript">
<!--
var myArray = new Array("red",4,"blue",13,"green",5);
document.write("<h3>原数组为:"+myArray);
var newArray = myArray.sort(myFunction);
document.write("<h3>新数组为:"+newArray);
//-->
</script>
</body>
</html>
结果:
原数组为:red,4,blue,13,green,5
新数组为:blue,green,red,4,5,13
六:倒序数组
var newArray = myArray.reverse();
document.write("<h3>使用reverse方法的倒序数组为:"+newArray);
结果为:
原数组为:Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday
使用for循环语句的倒序数组为:Saturday Friday Thursday Wednesday Tuesday Monday Sunday
使用reverse方法的倒序数组为:Saturday,Friday,Thursday,Wednesday,Tuesday,Monday,Sunday