# JavaScript數組簡介

## 對數組的簡要介紹，它是JavaScript的關鍵構建塊

JavaScript中的數組不是類型他們自己。

``````const a = []
const a = Array()
``````

``````const a = [1, 2, 3]
const a = Array.of(1, 2, 3)
``````

``````const a = [1, 'Flavio', ['a', 'b']]
``````

``````const matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
matrix[0][0] //1
matrix[2][0] //7
``````

``````a[0] //1
a[1] //2
a[2] //3
``````

``````Array(12).fill(0)
``````

``````const a = [1, 2, 3]
a.length //3
``````

``````const a = [1, 2, 3]
a //[ 1, 2, 3 ]
a.length = 2
a //[ 1, 2 ]
``````

## 如何將項目添加到數組

``````a.push(4)
``````

``````a.unshift(0)
a.unshift(-2, -1)
``````

## 如何從數組中刪除項目

``````a.pop()
``````

``````a.shift()
``````

## 如何加入兩個或多個數組

``````const a = [1, 2]
const b = [3, 4]
const c = a.concat(b) //[1,2,3,4]
a //[1,2]
b //[3,4]
``````

``````const a = [1, 2]
const b = [3, 4]
const c = [...a, ...b]
c //[1,2,3,4]
``````

## 如何在數組中查找特定項目

``````a.find((element, index, array) => {
//return true or false
})
``````

``````a.find(x => x.id === my_id)
``````

`findIndex()`類似於`find()`，但返回返回true的第一項的索引，如果找不到，則返回`-1`

``````a.findIndex((element, index, array) => {
//return true or false
})
``````

``````a.includes(value)
``````

``````a.includes(value, i)
``````