js內置對象:Array String Math Date <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> // 1、構造函數 ...
js內置對象:Array String Math Date
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> // 1、構造函數方法創建數組 var arr=new Array(); console.log(arr);//[] var arr2=new Array(3);//指定數組個數 console.log(arr2);//(3) [empty × 3] var arr3=new Array(1,2,3);//指定數組元素 console.log(arr3);//(3) [1, 2, 3] // 2、字面量表示法創建數組 var arr4=[1,2,3,4]; console.log(arr4);//(4) [1, 2, 3, 4] </script> </body> </html>
讀取和設置數組元素
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> var arr4=[1,2,3,4]; console.log(arr4[0]);//1 arr4[0]=0; console.log(arr4);//(4) [0, 2, 3, 4] </script> </body> </html>
數組長度=數組最大索引值+1
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> var arr4=[1,2,3,4]; console.log(arr4.length);//4 獲取數組長度 arr4.length=3; console.log(arr4);//(3) [1, 2, 3] arr4[9]=9; console.log(arr4);//(10) [1, 2, 3, empty × 6, 9] console.log(arr4.length);//10 數組長度永遠=最大索引值+1 // 遍曆數組 var len=arr4.length; for(var i=0;i<len;i++){ console.log(arr4[i]); } </script> </body> </html>
數組的棧方法:
push() 從數組最後添加元素,返回數組的新長度
unshift() 從數組的開頭添加元素,返回數組的新長度
pop() 刪除數組的最後一個元素,返回被刪除的那個元素
shift() 刪除數組的第一個元素,返回被刪除的那個元素
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> var arr=[1,2,3,4]; var len=arr.push(5,6); console.log(len);//6 console.log(arr);//(6) [1, 2, 3, 4, 5, 6] var arr1=[1,2,3,4]; var len1=arr1.unshift(-1,0); console.log(len1);//6 console.log(arr1);//(6) [-1, 0, 1, 2, 3, 4] var arr2=[1,2,3,4]; var n2=arr2.pop(); console.log(n2);//4 console.log(arr2);//(3) [1, 2, 3] var arr3=[1,2,3,4]; var n3=arr3.shift(); console.log(n3);//1 console.log(arr3);//(3) [2, 3, 4] </script> </body> </html>
數組方法:
join() 數組轉字元串
預設分隔符是逗號,可以自定義
reverse() 翻轉數組中的元素順序
sort() 數組排序,預設隱式轉換為字元串,然後進行排序
可以自定義參數,用來進行數值的排序
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> var arr=[1,2,3,4]; var str=arr.join(); console.log(str);// 字元串 1,2,3,4 var arr2=[1,2,3,4]; var str2=arr2.join(''); console.log(str2);// 字元串 1234 var arr3=[1,2,3,4]; var str3=arr3.join('-'); console.log(str3);// 字元串 1-2-3-4 var arr4=[1,2,3,4]; var arr4=arr4.reverse(); console.log(arr4);// (4) [4, 3, 2, 1] var arr5=[1,24,3,42]; var arr5=arr5.sort(); console.log(arr5);// (4) [1, 24, 3, 42] var arr6=[1,24,3,42]; var arr6=arr6.sort(function(a,b){return a-b;}); console.log(arr6);// (4) [1, 3, 24, 42] var arr7=[1,24,3,42]; var arr7=arr7.sort(function(a,b){return b-a;}); console.log(arr7);// (4) [42, 24, 3, 1] </script> </body> </html>
concat() 數組合併
slice(start, end) 數組截取,包含start不包含end
slice(start) 預設從start位置截取到最後
如果start或者end為負數,則等價於 該負數+數組長度
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> var arr1=[1,2,3]; var arr2=[4,5,6]; var newArr=arr1.concat(arr2); console.log(newArr);//(6) [1, 2, 3, 4, 5, 6] var arr=[0,1,2,3,4,5]; var slice1=arr.slice(2); console.log(slice1);//(4) [2, 3, 4, 5] var slice2=arr.slice(2,5); console.log(slice2);//(3) [2, 3, 4] var slice3=arr.slice(-5); console.log(slice3);//(5) [1, 2, 3, 4, 5] var slice4=arr.slice(-5,5); console.log(slice4);//等價於(1,5) (4) [1, 2, 3, 4] </script> </body> </html>
數組的拷貝,方法越多越好
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> //數組拷貝,方法越多越好 var a=[1,2,3,4]; var b; b=a; console.log(b); b=a.concat([]); console.log(b); b=a.slice(0); console.log(b); b=[]; for(var i=0;i<a.length;i++){ b.push(a[i]); } console.log(b); </script> </body> </html>
splice(index, count, item...)
splice(index, count) 從Index位置開始,刪除count個數據
splice(index, 0, item...) 從index位置開始,添加item...
splice(index, count, item...) 從Index位置開始,刪除count個數據,並插入item...
返回值始終是被刪除的數據
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> var arr=['a', 'b', 'c', 'd', 'e', 'f', 'g']; var newArr=arr.splice(2, 2); console.log(newArr);//(2) ["c", "d"] console.log(arr);//(5) ["a", "b", "e", "f", "g"] var arr2=['a', 'b', 'c', 'd', 'e', 'f', 'g']; var newArr2=arr2.splice(2); console.log(newArr2);//(5) ["c", "d", "e", "f", "g"] console.log(arr2);//(2) ["a", "b"] var arr3=['a', 'b', 'c', 'd', 'e', 'f', 'g']; var newArr3=arr3.splice(2, 0, 'm', 'n'); console.log(newArr3);//[] console.log(arr3);//["a", "b", "m", "n", "c", "d", "e", "f", "g"] var arr4=['a', 'b', 'c', 'd', 'e', 'f', 'g']; var newArr4=arr4.splice(2, 2, 'm', 'n'); console.log(newArr4);//(2) ["c", "d"] console.log(arr4);//(7) ["a", "b", "m", "n", "e", "f", "g"] </script> </body> </html>
indexOf(value, start) 檢測數據在數組中第一次出現的位置
如果有start值,則檢測位置從start處開始
lastIndexOf(value) 檢測數據在數組中最後一次出現的位置
不存在則為-1
相容性:IE9+
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> var arr=['a', 'b', 'c', '