陣列資料格式
1
2
3
4
5
|
let fruits = ["蘋果","香蕉","鳳梨","蓮霧"];
console.log(fruits[0]); //蘋果
console.log(fruits[1]); //香蕉
console.log(fruits[2]); //鳳梨
console.log(fruits[3]); //蓮霧
|
將陣列資料塞進html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
let fruits = ["蘋果","香蕉","鳳梨","蓮霧"];
//單行式寫法
$("ul").append("<li>蘋果</li>");
$("ul").append("<li>香蕉</li>");
$("ul").append("<li>鳳梨</li>");
$("ul").append("<li>蓮霧</li>");
//for迴圈寫法
for(let i=0 ; i<fruits.length ; i++){
$("ul").append("<li>"+ fruits[i] +"</li>");
}
//forEach寫法
fruits.forEach(function(fruit){
$("ul").append("<li>" + fruit + "</li>")
});
|
編譯結果:
1
2
3
4
5
6
|
<ul>
<li>蘋果</li>
<li>香蕉</li>
<li>鳳梨</li>
<li>蓮霧</li>
</ul>
|
for(從第幾個開始,到第幾個結束,每一次要間隔多少)
新增資料(push)
1
2
3
4
5
|
let fruits = ["蘋果","香蕉","鳳梨","蓮霧"];
fruits.push("榴槤");
console.log(fruits); //["蘋果","香蕉","鳳梨","蓮霧","榴槤"]
|
新增資料2(push)
1
2
3
4
5
|
let fruits = ["蘋果","香蕉","鳳梨","蓮霧"];
fruits.push("榴槤","酪梨");
console.log(fruits); //["蘋果","香蕉","鳳梨","蓮霧","榴槤","酪梨"]
|
其他相關