发新话题
打印

JavaScript中,定义数组的方法

JavaScript中,定义数组的方法

定义一个数组变量:
var a=new Array();

定义时指定大小:
var a=new Array(20);

定义时赋初值:
var a=new Array('abc', 'def', 'ghi', 'opq');

TOP

javascript定义二维数组的方法

/定义模拟二维数组的函数
function Array2DVar(x,y)
{
this.length = x
this.x = x
this.y = y
for(var j = 0; j < this.length; j++)
this[j] = new Array(y)
}
调用方法

var N=6

var a = new Array2DVar(N+1,N+1)
var b = new Array2DVar(N+1,N+1)

关于数组的其他用法见下

Array Object Methods
数组的方法
The concat() method is used to join two or more arrays.
组合两个或者更多得数组
This method does not change the existing arrays, it only returns a copy of the joined arrays.
arrayObject.concat(arrayX,arrayX,......,arrayX)

Required. One or more array objects to be joined to an array
Example 1:
Here we create two arrays and show them as one using concat():

<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>

The output of the code above will be:
Jani,Tove,Hege,John,Andy,Wendy
Example 2
Here we create three arrays and show them as one using concat():
<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"

var arr3 = new Array(2)
arr3[0] = "Stale"
arr3[1] = "Borge"
document.write(arr.concat(arr2,arr3))
</script>

The output of the code above will be:
Jani,Tove,Hege,John,Andy,Wendy,Stale,Borge
join()方法是把数组组和为一个 字符串
The join() method is used to put all the elements of an array into a string.

The elements will be separated by a specified separator.

用法arrayObject.join(separator):
Note: Comma is the default separator for the join() method.
Example
In this example we will create an array, and then put all the elements in a string:

<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>
The output of the code above will be:
Jani,Hege,Stale
Jani.Hege.Stale
pop()方法
The pop() method is used to remove and return the last element of an array.
arrayObject.pop()
Note: This method changes the length of the array.

Tip: To remove and return the first element of an array, use the shift() method.
Example
In this example we will create an array, and then remove the last element(元素) of the array. Note that this will also change the length of the array:

<script type="text/javascript">
var arr = new Array(3)
arr[0] = "Jani"
arr[1] = "Hege"
arr[2] = "Stale"
document.write(arr + "<br />")
document.write(arr.pop() + "<br />")
document.write(arr)
</script>
The output of the code above will be:

Jani,Hege,Stale
Stale
Jani,Hege
push方法 增加元素的方法
The push() method adds one or more elements to the end of an array and returns the new length.

arrayObject.push(newelement1,newelement2,....,newelementX)

newelement1 Required. The first element to add to the array
newelement2 Optional. The second element to add to the array
newelementX Optional. Several elements may be added
Tips and Notes

Note: This method changes the length of the array.

Tip: To add one or more elements to the beginning of an array, use the unshift() method.
Example
In this example we will create an array, and then change the length of it by adding a element:
<script type="text/javascript">
var arr = new Array(3)
arr[0] = "Jani"
arr[1] = "Hege"
arr[2] = "Stale"
document.write(arr + "<br />")
document.write(arr.push("Kai Jim") + "<br />")
document.write(arr)
</script>
The output of the code above will be:

Jani,Hege,Stale
4(说明该处的为数组的长度值)
Jani,Hege,Stale,Kai Jim
reverse() 方法倒序输出
The reverse() method is used to reverse the order of the elements in an array.
arrayObject.reverse()
Note: The reverse() method changes the original array.
Example
In this example we will create an array, and then reverse the order of it:

<script type="text/javascript">
var arr = new Array(3)
arr[0] = "Jani"
arr[1] = "Hege"
arr[2] = "Stale"
document.write(arr + "<br />")
document.write(arr.reverse())
</script>
The output of the code above will be:
Jani,Hege,Stale
Stale,Hege,Jani
The shift() 删除数组第一个元素
The shift() method is used to remove and return the first element of an array.
arrayObject.shift()
Tips and Notes
Note: This method changes the length of the array.

Tip: To remove and return the last element of an array, use the pop() method.

Example
In this example we will create an array, and then remove the first element of the array. Note that this will also change the length of the array:

<script type="text/javascript">
var arr = new Array(3)
arr[0] = "Jani"
arr[1] = "Hege"
arr[2] = "Stale"
document.write(arr + "<br />")
document.write(arr.shift() + "<br />")
document.write(arr)
</script>
The output of the code above will be:

Jani,Hege,Stale
Jani
Hege,Stale
The slice() 方法返回 第几个开始的数组
The slice() method returns selected elements from an existing array.

arrayObject.slice(start,end)

start Required. Specify where to start the selection. Must be a number
end Optional. Specify where to end the selection. Must be a number
Tips and Notes
Tip: You can use negative numbers to select from the end of the array.

Note: If end is not specified, slice() selects all elements from the specified start position and to the end of the array.

Example
In this example we will create an array, and then display selected elements from it:

<script type="text/javascript">

var arr = new Array(3)
arr[0] = "Jani"
arr[1] = "Hege"
arr[2] = "Stale"

document.write(arr + "<br />")
document.write(arr.slice(1) + "<br />")
document.write(arr)

</script>
The output of the code above will be:
Jani,Hege,Stale
Hege,Stale
Jani,Hege,Stale


Example
In this example we will create an array, and then display selected elements from it:
<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.slice(2,4) + "<br />")
document.write(arr)

</script>
The output of the code above will be:

Jani,Hege,Stale,Kai Jim,Borge,Tove
Stale,Kai Jim
Jani,Hege,Stale,Kai Jim,Borge,Tove
The sort()方法排序
The sort() method is used to sort the elements of an array.
Parameter /Description
sortby Optional. Specifies the sort order. Must be a function
Tips and Notes
Note: The sort() method will sort the elements alphabetically by default. However, this means that numbers will not be sorted correctly (40 comes before 5). To sort numbers, you must create a function that compare numbers.

Note: After using the sort() method, the array is changed.

Example
In this example we will create an array and sort it alphabetically:

<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>
The output of the code above will be:

Jani,Hege,Stale,Kai Jim,Borge,Tove
Borge,Hege,Jani,Kai Jim,Stale,Tove
Example
In this example we will create an array containing numbers and sort it:

<script type="text/javascript">
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())</script>
The output of the code above will be:
10,5,40,25,1000,1
1,10,1000,25,40,5
Note that the numbers above are NOT sorted correctly (by numeric value). To solve this problem, we must add a function that handles this problem:
<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>
The output of the code above will be:

10,5,40,25,1000,1
1,5,10,25,40,1000
The splice()方法
The splice() method is used to remove and add new elements to an array.

arrayObject.splice(index,howmany,element1,.....,elementX)

Parameter Description
index Required. Specify where to add/remove elements. Must be a number
howmany Required Specify how many elements should be removed. Must be a number, but can be "0"
element1 Optional. Specify a new element to add to the array
elementX Optional. Several elements can be added
Example
In this example we will create an array and add an element to it:

<script type="text/javascript">
var arr = new Array(5)
arr[0] = "Jani"
arr[1] = "Hege"
arr[2] = "Stale"
arr[3] = "Kai Jim"
arr[4] = "Borge"
document.write(arr + "<br />")
arr.splice(2,0,"Lene")
document.write(arr + "<br />")
</script>
The output of the code above will be:

Jani,Hege,Stale,Kai Jim,Borge
Jani,Hege,Lene,Stale,Kai Jim,Borge
arr.splice(2,0,"Lene")
如果上面的参数设置为arr.splice(2,1,"Lene")那么结果为 直接替换了元素
Jani,Hege,Stale,Kai Jim,Borge
Jani,Hege,Lene,Kai Jim,Borge

Example
In this example we will remove the element at index 2 ("Stale"), and add a new element ("Tove") there instead:
<script type="text/javascript">
var arr = new Array(5)
arr[0] = "Jani"
arr[1] = "Hege"
arr[2] = "Stale"
arr[3] = "Kai Jim"
arr[4] = "Borge"
document.write(arr + "<br />")
arr.splice(2,3,"Tove")
document.write(arr)
</script>
The output of the code above will be:

Jani,Hege,Stale,Kai Jim,Borge
Jani,Hege,Tove
The toSource()
The toSource() method represents the source code of an object.
object.toSource()
Tips and Notes
Note: This method does not work in Internet Explorer!

Example
In this example we will show how to use the toSource() method:

<script type="text/javascript">
function employee(name,jobtitle,born)
{
this.name=name
this.jobtitle=jobtitle
this.born=born
}
var fred=new employee("Fred Flintstone","Caveman",1970)
document.write(fred.toSource())
</script>
The output of the code above will be:
({name:"Fred Flintstone", jobtitle:"Caveman", born:1970})
toString() 方法的使用
The toString() method converts an array to a string and returns the result.
arrayObject.toString()
Tips and Notes
Note: The elements in the array will be separated with commas.

Example
In this example we will create an array and convert it to a string:

<script type="text/javascript">
var arr = new Array(3)
arr[0] = "Jani"
arr[1] = "Hege"
arr[2] = "Stale"
document.write(arr.toString())
</script>

The output of the code above will be:

Jani,Hege,Stale

The unshift() method adds one or more elements to the beginning of an array and returns the new length.
arrayObject.unshift(newelement1,newelement2,....,newelementX)

Parameter Description
newelement1 Required. The first element to add to the array
newelement2 Optional. The second element to add to the array
newelementX Optional. Several elements may be added
Tips and Notes
Note: This method changes the length of the array.

Note: The unshift() method does not work properly in Internet Explorer!

Tip: To add one or more elements to the end of an array, use the push() method.

Example
In this example we will create an array, add an element to the beginning of the array and then return the new length:

<script type="text/javascript">
var arr = new Array()
arr[0] = "Jani"
arr[1] = "Hege"
arr[2] = "Stale"
document.write(arr + "<br />")
document.write(arr.unshift("Kai Jim") + "<br />")
document.write(arr)
</script>
The output of the code above will be:

Jani,Hege,Stale
4
Kai Jim,Jani,Hege,Stale
The valueOf()方法的使用
The valueOf() method returns the primitive value of an Array object.

The primitive value is inherited by all objects descended from the Array object.

The valueOf() method is usually called automatically by JavaScript behind the scenes and not explicitly in code.
使用方法arrayObject.valueOf()


Array Object Properties
数组的属性
Property Description /FF/ N /IE  
constructor A reference to the function that created the object 1 /2/ 4
index   1/ 3 /4
input   1 /3/ 4
length Sets or returns the number of elements in an array 1/ 2/ 4
prototype Allows you to add properties and methods to the object 1 /2/ 4

The constructor property is a reference to the function that created an object.
使用方法object.constructor
Example 1
In this example we will show how to use the constructor property:

<script type="text/javascript">
var test=new Array()
if (test.constructor==Array)
{document.write("This is an Array")}
if (test.constructor==Boolean)
{document.write("This is a Boolean")}
if (test.constructor==Date)
{document.write("This is a Date")}
if (test.constructor==String)
{document.write("This is a String")}

</script>

The output of the code above will be:

This is an Array

Example 2
In this example we will show how to use the constructor property:
<script type="text/javascript">
function employee(name,jobtitle,born)
{
this.name=name
this.jobtitle=jobtitle
this.born=born
}
var fred=new employee("Fred Flintstone","Caveman",1970)
document.write(fred.constructor)
</script>

The output of the code above will be:

function employee(name, jobtitle, born)
{this.name = name; this.jobtitle = jobtitle; this.born = born;}

The length property sets or returns the number of elements in an array.

使用方法arrayObject.length
Example
In this example we will show how to use the length property to both return and set the length of an array:

<script type="text/javascript">
var arr = new Array(3)
arr[0] = "John"
arr[1] = "Andy"
arr[2] = "Wendy"
document.write("Original length: " + arr.length)
document.write("<br />")
arr.length=5
document.write("New length: " + arr.length)
</script>

The output of the code above will be:
Original length: 3
New length: 5

The prototype property allows you to add properties and methods to an object.
object.prototype.name=value
Example 1
In this example we will show how to use the prototype property to add a property to an object:

<script type="text/javascript">

function employee(name,jobtitle,born)
{
this.name=name
this.jobtitle=jobtitle
this.born=born
}
var fred=new employee("Fred Flintstone","Caveman",1970)

employee.prototype.salary=null

fred.salary=20000

document.write(fred.salary)

</script>

The output of the code above will be:

20000

TOP

发新话题