Properties and methods of Array objects in JavaScript
Array object: an array variable is an array object
length property: dynamically obtains the length of the array. For example: var len = arrObj.length
##join()
- Function: Convert an array into a string. Returns a string.
- Syntax: arrObj.join (connection number)
- Description: Connect an array into one character using the specified "connection number" string.
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> //將以下字符串轉(zhuǎn)成數(shù)組, 然后再轉(zhuǎn)成字符串 var str = "北京,上海,深圳,南京,合肥"; //轉(zhuǎn)成數(shù)組 var arr=str.split(","); //再轉(zhuǎn)換成字符串 str = arr.join(",") ; document.write("類型是:"+typeof(str)+",字符串為:"+str); </script> </head> <body> </body> </html>
##reverse()
- Function: Reverse the order of elements in the array.
- Syntax: arrObj.reverse()
- Parameters: None
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> var arr=[1,2,3,4,5,6,7,8]; arr.reverse(); document.write(arr); </script> </head> <body> </body> </html>
Deletion and addition of array elements
- delete operation symbol, only the value of the array element can be deleted, but the space occupied is still there, and the total length has not changed (arr.length).
- #Previous array elements can only be added backward, not forward.
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> //使用delete刪除元素,查看長度是否改變 var arr=[1,2,3,4,5,6,7,8]; document.write("數(shù)組的長度為"+arr.length+",值為"+arr+"<br/>"); delete arr[0]; delete arr[1]; delete arr[2]; document.write("數(shù)組的長度為"+arr.length+",值為"+arr+"<br/>"); </script> </head> <body> </body> </html>
- shift(): Delete the first element in the array, return the deleted value, and reduce the length by 1.
- pop(): Delete the last element in the array, return the deleted value, and reduce the length by 1.
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> //使用delete刪除元素,查看長度是否改變 var arr=[1,2,3,4,5,6,7,8]; document.write("數(shù)組的長度為"+arr.length+",值為"+arr+"<br/>"); arr.shift(); document.write("數(shù)組的長度為"+arr.length+",值為"+arr+"<br/>"); arr.pop(); document.write("數(shù)組的長度為"+arr.length+",值為"+arr+"<br/>"); </script> </head> <body> </body> </html>
- unshift(): Add one or more array elements to the front of the array, and the length needs to be changed. arrObj.unshift("a", "b", "c")
- push(): Add one or more array elements to the end of the array, length To change. arrObj.push(“a” , “b” , “c”)
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> //使用delete刪除元素,查看長度是否改變 var arr=[1,2,3,4,5,6,7,8]; document.write("數(shù)組的長度為"+arr.length+",值為"+arr+"<br/>"); arr.unshift(0); document.write("數(shù)組的長度為"+arr.length+",值為"+arr+"<br/>"); arr.push(9); document.write("數(shù)組的長度為"+arr.length+",值為"+arr+"<br/>"); </script> </head> <body> </body> </html>