-
Notifications
You must be signed in to change notification settings - Fork 0
/
js0609_push_pop_array.html
90 lines (70 loc) · 2.6 KB
/
js0609_push_pop_array.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
array_push_pop_practice_0609_class_note大彙整&額外自己增加
<script>
//1.push //"加入項目至陣列末端"
//let pushArr = [1,3,5,7,9];
//pushArr.push(55688);
//console.log('pushArr',pushArr);
//額外小提醒:當 reduce() 搭配 push() 或 concat() 使用時,
//要注意 push() 會改變原陣列,回傳的是新陣列的長度(length),
//concat() 才會回傳的是被連結後的結果。
//pop //"移除陣列末端項目"
//let popArr = [1,3,5,7,9];
//popArr.pop();
//console.log('popArr',popArr);
//==========額外作筆記
//ex: myArr[i] vs myArr ['i']
//上述這兩個比較是不一樣的!
//length 長度
//console.log(pushArr.length);
//index change array
//pushArr[1] = 12345;
//console.log(12345,pushArr);
//shift vs unshift //unshift "就是加入項目至陣列前端"
//shift 就是"刪除第一個"
//let shiftArr = [1,2,3];
//shiftArr.shift();
//console.log('shiftArr',shiftArr);
//unshift : "增加value 到 array最前面"
//let unshiftArr = [1,2,3,4,5];
//unshiftArr.unshift(4);
//consloe.log('unshiftArr',unshifArr);
//splice
//let splicArr = [1,2,3,4,5,6,7,8];
//spliceArr.splice(4);
//position
//移除數量 splice()方法就是可以藉由刪除既有元素,或加入新元素來改變一個陣列的內容。
//spliceArr.splice(0,2);
//console.log('spliceArr',spliceArr);
//toString() //這個就是轉字串
//array =>string ajax 傳送資料 api不收array 只收字串
//let stringArr = ['apple','banana','cclemon'];
//let getString = stringArr.toString();
//console.log(getString);
//array
//let arr1 = [1,2,3];
//let arr2 = [7,8,9];
//let arr3 = arr1.concat(arr2);
//console.log('ar3',arr3);
//分散運算子 spread oprator
//let arr4 = [...arr1, ....arr2];
//console.log('arr4', arr4);
//indexof (找出排序) 也就是在陣列中尋找項目的索引
//console.log('arrIndexOf',arrIndexOf);
//console.log(arrIndexOf.indexOf('amy')); //0
//console.log(arrIndexOf.indexOf('bob')); //1
//console.log(arrIndexOf.indexOf('cat')); //2
//console.log(arrIndexOf.indexOf('dog')); //-1 不存在
//複製陣列 slice
//var shallowCopy = fruits.slice(); // 這就是複製陣列的方式
</script>
</body>
</html>