---
# System prepended metadata

title: 陣列
tags: [JavaScript]

---

# 陣列

* array.length

```javascript
let fruits = [];
//寫入
fruits[0]="apple";
fruits[2]="orange";

console.log(fruits);
console.log(fruits[1]);
console.log(fruits.length);
```
* array.push：新增陣列中**最後一筆**資料
* array.unshift：新增陣列中**第一筆**資料
```javascript
let fruits = ["apple","orange","grape"];
//寫入
fruits.push("tomato");
fruits.push("banana");

console.log(fruits); 
console.log(fruits.length);
```

console.log結果為

```
["apple","orange","grape","tomato","banana"]
5
```

* array.pop：刪除陣列中**最後一筆**資料
* array.shift：刪除陣列中**第一筆**資料
```javascript
let fruits = ["apple","orange","grape"];

fruits.unshift("tomato");
fruits.unshift("banana");
console.log(fruits);

fruits.pop();
fruits.shift();
console.log(fruits);
console.log(fruits.length);
```
* array.splice：刪除指定資料
第一數字是指起始位置，第二個數字是指刪除幾筆資料，包含自己
```javascript
fruits.shift(1,1);
```