JavaScript數(shù)組對象屬性length和二維數(shù)組
數(shù)組對象屬性length
一個數(shù)組,其實就是一個“數(shù)組對象”。把數(shù)組看成“對象”,是為了使用數(shù)組對象的屬性或方法。
在JS中,所有的內(nèi)容都是“對象”。
那么,length就是數(shù)組對象的一個屬性。如:var len = arrObj.length;
length屬性,可以動態(tài)獲取數(shù)組的長度。
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> var arr = ["濤哥" , "男" , 24, "大專" , "北京科技大學" ]; document.write(arr.length); </script> </head> <body> </body> </html>
二維數(shù)組
給一個數(shù)組元素,賦一個數(shù)組的值,那么,這個數(shù)組就是“二維數(shù)組”。
創(chuàng)建一個簡單的二維數(shù)組:
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> //使用[]方式創(chuàng)建一個數(shù)組 var arr = [ [1,2,3,4], [4,5,6,7], [8,9,10,11] ]; document.write(arr); </script> </head> <body> </body> </html>
二維數(shù)組,就得用兩層循環(huán)來實現(xiàn)。也就是說循環(huán)套循環(huán)。
二維數(shù)組的訪問:數(shù)組名后跟多個連續(xù)的中括號[],第1個中括號[]代表第一維數(shù)組,第2個中括號[]代表第二維數(shù)組。
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> //使用[]方式創(chuàng)建一個數(shù)組 var arr = [ [1,2,3,4], [4,5,6,7], [8,9,10,11] ]; document.write(arr[1][1]); </script> </head> <body> </body> </html>