JavaScript | 數組

———————————————————————————————————————————— 數組

數組 函數

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -spa

數組結構 3d

  • JavaScript的數組由索引和值組成

  • 索引便可以是正整數,也能夠是其餘類型,但其餘類型索引不算在數組長度內

  • 當數組索引不連續時,又稱爲稀疏數組,但相比較連續數組來講,稀疏數組查找元素的速度較慢,未定義部分爲undefined


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - code

建立數組:兩種形式 blog

  • 數組字面量
  • 構造函數Array()建立數組

遍歷數組:三種形式 索引

  • for循環遍歷下標連續的數組
  • for-in遍歷數組
  • forEach()構造函數遍歷數組
 1 // 數組字面量的形式
 2 var arr1 = [];
 3 // 數組中能夠存放全部的變量形式
 4 var arr2 = [1, 2.3, null, true, false, undefined, [1, 2, 3, 4], { x: 1, y: 2, z: 3 }];
 5 console.log(arr2);
 6 console.log(arr2[6][1]);
 7 console.log(arr2[7].y);
 8 // 打印數組長度
 9 console.log(arr2.length);
10 // 引用內容來建立數組
11 var num = 1;
12 var arr3 = [num, num + 1, num * 2];
13 console.log(arr3);
14 // 省略數組中的某個元素,默認爲undefined
15 var arr4 = [1, , 3];
16 console.log(arr4);
17 
18 // 經過構造函數的方式
19 var arr5 = new Array;
20 // 多個參數時定義的是數組的內容
21 var arr6 = new Array(1, 2, 3);
22 console.log(arr6);
23 // 只傳一個參數時定義的是數組的長度
24 var arr7 = new Array(6);
25 console.log(arr7);
26 console.log(arr7.length);
27 arr7[9] = '1';
28 console.log(arr7);
29 console.log(arr7.length);
30 // 任何變量均可以做爲索引值,但只有非負整數做爲索引時長度纔會變化
31 var arr8 = new Array();
32 arr8[2.3] = 'a';
33 arr8[-2333] = 'b';
34 arr8['c'] = 'b';
35 // 經過for in取出元素以及元素的索引
36 for (var i in arr8) {
37     console.log(i + ":" + arr8[i]);
38 }
39 console.log(arr8);
40 console.log(arr8.length);
41 arr8[2] = 'd';
42 console.log(arr8);
43 console.log(arr8.length);
44 // 壓棧彈棧操做
45 var arr9 = new Array(1, 2, 3);
46 arr9.push(4, 5, 6, 7);
47 console.log(arr9);
48 console.log(arr9.length);
49 arr9.pop();
50 arr9.pop();
51 console.log(arr9);
52 console.log(arr9.length);
53 // 在首部加入元素
54 arr9.unshift(9, 10, 11, 12);
55 console.log(arr9);
56 // 首部彈出元素
57 arr9.shift();
58 console.log(arr9);
59 // 刪除元素,但刪除後長度不變
60 delete arr9[3];
61 console.log(arr9);
62 console.log(arr9.length);
63 // 稀疏數組遍歷
64 var arr10 = [1, 2, 3];
65 arr10[100] = 99;
66 // for-in遍歷集成下來的屬性
67 for (var i in arr10) {
68     console.log(i);
69 }
70 // 在forEach()中定義函數體,
71 arr10.forEach(Test);
72 
73 function Test(element, index, array) {
74     console.log("array:" + array + " index:" + index + " element:" + element);
75 }

經常使用方法ip

 1 // 經過.map方法返回新函數,在map方法中調用Trans函數,對數組中的每個元素進行替換操做
 2 var arr = ['abc', 'bcd', 'cde'];
 3 res = arr.map(Trans);
 4 
 5 function Trans(x) {
 6     return x.replace(/c/g, '!').toUpperCase();
 7 }
 8 console.log(res);
 9 
10 // filter
11 var arr = [1, 3, 4, 5, 6, 6, 123, 6547, null, undefined, ""];
12 res = arr.filter(function(x) {
13     return (x <= 10) && (x != null) && (x != "");
14 })
15 console.log(res);
16 
17 // reduce做爲累加器,從左到右依次進行返回運算
18 var arr = [1, 2, 3, 4, 5, 6, 7, 8];
19 res = arr.reduce(function(a, b) {
20     return a - b;
21 })
22 console.log(res);
23 res = arr.reduce(function(a, b) {
24     return a * b;
25 })
26 console.log(res);
27 // reduceRight從右往左
28 res = arr.reduceRight(function(a, b) {
29     return a - b;
30 })
31 console.log(res);
32 
33 // every函數檢測是否全部的元素都符合要求,有不符合的則返回false
34 var age = [12, 34, 55, 4];
35 res = age.every(function(x) {
36     return x >= 18;
37 })
38 console.log(res);
39 // some檢測的是否有符合的,有符合的則返回true
40 var age = [12, 34, 55, 4];
41 res = age.some(function(x) {
42     return x >= 18;
43 })
44 console.log(res);
45 
46 // 索引
47 var arr = ['a','b','c','b','d','b','e'];
48 // 第一個索引位
49 res = arr.indexOf('b');
50 console.log(res);
51 // 最後一個索引位
52 res = arr.lastIndexOf('b');
53 console.log(res);
54 // 索引開始位置
55 res = arr.indexOf('b',3);
56 console.log(res);

   

相關文章
相關標籤/搜索