๐Arrays
๋น ๋ฐฐ์ด ์์ฑ
let colors = [];
์ฌ๋ฌ ํ์
์ด ์
๋ ฅ๋ ๋ฐฐ์ด
let stuff = [true, 68, 'cat', null];
0 1 2 3
๊ฐ๊ฐ์ ์์๋ ์์ํ๋ ์ธ๋ฑ์ค๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
js์์๋ ์ฌ๋ฌ ํ์
(๋ถ๋ฆฌ์ธ, ์ ์, ๋ฌธ์์ด)์ด ํจ๊ป ์๋ ๋ฐฐ์ด์ ๋ง๋ค ์ ์๋ค
๋ฐฐ์ด ์์ ์ ๊ทผ
input stuff[0]
output "true"
input stuff[2][0]
output "c"
๋ฐฐ์ด ์์
input stuff[1] = 'red'
input stuff
output [true, 'red, 'cat, null]
+) ๋ฌธ์์ด์์๋ ํน์ ๋ฌธ์๋ฅผ ๋ฐ๊ฟ์ ์๋ค
๐Array Methods(Push, pop, shift, unshift)
Push = add to end
let stuff = [true, 68, 'cat', null];
input stuff.push('oliver','harry')
input stuff
output [true, 68, "cat", null,"oliver","harry"];
pop = remove from end
input stuff.pop()
output "harry"
input stuff
output [true, 68, "cat", null,"oliver"];
Shift = remove from start
Unshift = add to start
์ ์ฉ ์์น๋ง ๋ค๋ฅด์ง ์ฌ์ฉ ๋ฐฉ๋ฒ์ ๊ฐ๋ค
๐More Array Methods (concat, indexOf, includes, reverse, slice, splice)
concat = ๋ฐฐ์ด ๋ณํฉ
let cats = ['blue', 'kitty']
let dogs = ['rusty', 'wyatt']
let comboParty = cats.concat(dogs)
input comboParty
output ["blue", "kitty", "rusty", "wyatt"]
includes = ํน์ ๊ฐ์ด ํฌํจ๋์ด ์๋์ง ์ฌ๋ถ
input cats.includes('blue')
output true
indexOf = ํน์ ๊ฐ์ ์์น๋ฅผ ์๋ ค์ค๋ค ๋ํ, ์กด์ฌํ๋์ง์ ์ฌ๋ถ๋ ์ ์ ์๋ค
input comboParty.indexOf('kitty')
output 3
input comboParty.indexOf('hong')
output -1
+)๋ง์ฝ ๋ฐฐ์ด์์ kitty๊ฐ ๋ ๊ฐ ์กด์ฌํ๋ค๋ฉด ๋จผ์ ์ฐพ์ kitty์ ์ธ๋ฑ์ค๋ฅผ ์๋ ค์ค๋ค
reverse = ๋ฐฐ์ด ์์๋ฅผ ๋ฐ๊พผ๋ค
comboParty.reverse()
0 1 2 3 => 3 2 1 0
slice = ๋ฐฐ์ด์ ์ผ๋ถ๊ฐ ํ์ ํ ๋ ์ฌ์ฉํ๋ค
comboParty.slice()
์์์ ๊ณผ ๋์ ์ ์
๋ ฅํ ์ ์๋ค
(์์์ ์ ์์์์๋ฅผ ํฌํจํ์ง๋ง ๋์ ์ ๋์์๋ ์์๋ฅผ ํฌํจํ์ง ์๋๋ค)
input comboParty
output ["blue", "kitty", "rusty", "wyatt"]
์ซ์ ํ๋๋ง ์
๋ ฅํ๋ ๊ฒฝ์ฐ์๋ ์์์ ์ด ๋๋ค
input comboParty.slice(1,3)
output ["kitty", "rusty"]
์์๋ฅผ ์
๋ ฅํ๊ฒ ๋๋ฉด ๋์ผ๋ก ๊ฐ๋ค
input comboParty.slice(-2)
output ["rusty", "wyatt"]
let colors = [ "red", "orange","yellow", "green","blue","indigo","violet"]
splice = ๋ฐฐ์ด์ ์๋ ๊ฐ์ ์ ๊ฑฐ, ์ถ๊ฐ ,๋ณ๊ฒฝ ํ ์ ์๋ค
๋ค๋ฅธ method์ ๋ฌ๋ฆฌ ๋ณต์ฌ๋ณธ์ ๋ง๋ค์ง ์๊ณ ์๋ณธ์ ๋ณ๊ฒฝํ๋ค
colors.splice(3,1)
(์์์ ,์ ๊ฑฐ ํญ๋ชฉ ์)
colors.splice(1,0,'red-orange') = red์ orange ์ฌ์ด์
'red-orange'๊ฐ ์ถ๊ฐ ๋๋ค
(์์์ ,0,์ถ๊ฐํ ํญ๋ชฉ)
colors.splice(2,2,'black') = ์์์ ์์ 2๊ฐํญ๋ชฉ์ ์ง์ฐ๊ณ 'black'์ด ์ถ๊ฐ ๋๋ค
(์์์ ,์ ๊ฑฐ ํญ๋ชฉ ์, ์ถ๊ฐํ ํญ๋ชฉ)
sort
๐Reference type and Equality test
== (loose)
=== (strict)
๋ฐฐ์ด์ ๋น๊ตํ ๋๋ ์ฝํ
์ธ ๊ฐ ๋น๊ต๋๋๊ฒ ์๋๋ผ ์ฐธ์กฐ ์์น(๋ฉ๋ชจ๋ฆฌ ์ฃผ์)๊ฐ ๋น๊ต๋๋ ๊ฒ์ด๋ค
ex) [1,2,3] ==,=== [1,2,3]
false
let num = [1,2,3]
let numCopy = num
num === numCopy
true
๐Const and Arrays
const๋ฅผ ๋ฐฐ์ด์ ์ฌ์ฉํ ๋ ์ฐธ์กฐ ์์น๋ฅผ ๋ฐ๊พธ์ง ์๊ณ ๋ฐฐ์ด ์ ์ฝํ
์ธ ์ ๋ด์ฉ๋ฌผ์ ๋ฐ๊พธ๋ ๊ฒ์ ๊ฐ๋ฅํ๋ค
const nums = [1,2,3]
nums.pust(4) = ๊ฐ๋ฅํ๋ค
nums = [1,2,3,4] = ๋ถ๊ฐ๋ฅํ๋ค
๐Nested Arrays
const gameBoard = [[1,2,3],[4,5,6],[7,8,9]]
gameBoard[1][1]
5