JavaScript学习与实践(21)

JS中的数组对象,他用于使用一个变量名字存储一组值,

        例子:这个例子是创建一个数组,并分配了数值,最后打印出其中的值,

        

<html>
<body>
<script type="text/javascript">
var mycars = new Array()
mycars[0] = "Saab"
mycars[1] = "Volvo"
mycars[2] = "BMW"
for (i=0;i<mycars.length;i++)
{
document.write(mycars[i] + "<br />")
}
</script>
</body>
</html>
下面是一个for...in,怎么用他来循环便利数组中的每个元素呢?
<html>
<body>
<script type="text/javascript">
var x
var mycars = new Array()
mycars[0] = "Saab"
mycars[1] = "Volvo"
mycars[2] = "BMW"
for (x in mycars)
{
document.write(mycars[x] + "<br />")
}
</script>
</body>
</html>
下面是一个连接两个数组的方法concat()
<html>
<body>
<script type="text/javascript">
var arr = new Array(3)
arr[0] = "Jani"
arr[1] = "Tove"
arr[2] = "Hege"
var arr2 = new Array(3)
arr2[0] = "John"
arr2[1] = "Andy"
arr2[2] = "Wendy"
document.write(arr.concat(arr2))
</script>
</body>
</html>
 
下面是一个join()方法,他是把一个数组中所有的元素方到一个字符串中,,其中可以指定标点符号例子:
 
<html>
<body>
<script type="text/javascript">
var arr = new Array(3)
arr[0] = "Jani"
arr[1] = "Hege"
arr[2] = "Stale"
document.write(arr.join() + "<br />")
document.write(arr.join("."))
</script>
</body>
</html>
下面一个是分类,区别的sort(),他用于字面的,或者是数字的下面有两个例子来加以区别:
 
<html>
<body>
<script type="text/javascript">
var arr = new Array(6)
arr[0] = "Jani"
arr[1] = "Hege"
arr[2] = "Stale"
arr[3] = "Kai Jim"
arr[4] = "Borge"
arr[5] = "Tove"
document.write(arr + "<br />")
document.write(arr.sort())
</script>
</body>
</html>
 
第二个
 
 
<html>
<body>
<script type="text/javascript">
function sortNumber(a, b)
{
return a - b
}
var arr = new Array(6)
arr[0] = "10"
arr[1] = "5"
arr[2] = "40"
arr[3] = "25"
arr[4] = "1000"
arr[5] = "1"
document.write(arr + "<br />")
document.write(arr.sort(sortNumber))
</script>
</body>
</html>
上面这个我还没有理解,希望大家,指导我一下,谢谢
 

Array Object Methods

FF: Firefox, N: Netscape, IE: Internet Explorer

MethodDescriptionFFNIE
concat()Joins two or more arrays and returns the result144
join()Puts all the elements of an array into a string. The elements are separated by a specified delimiter134
pop()Removes and returns the last element of an array145.5
push()Adds one or more elements to the end of an array and returns the new length145.5
reverse()Reverses the order of the elements in an array134
shift()Removes and returns the first element of an array145.5
slice()Returns selected elements from an existing array144
sort()Sorts the elements of an array134
splice()Removes and adds new elements to an array145.5
toSource()Represents the source code of an object14-
toString()Converts an array to a string and returns the result134
unshift()Adds one or more elements to the beginning of an array and returns the new length146
valueOf()Returns the primitive value of an Array object124


Array Object Properties

PropertyDescriptionFFNIE 
constructorA reference to the function that created the object124
index 134
input 134
lengthSets or returns the number of elements in an array124
prototypeAllows you to add properties and methods to the object124

转载于:https://www.cnblogs.com/lxsohu/archive/2007/01/31/635626.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值