โญ๏ธ ๋ฐฐ์ด ๊ฐ์ฒด ( length ์์ฑ , join / push / pop ๋ฉ์๋ ) โญ๏ธ
01. ํ์ค ๋ด์ฅ ๊ฐ์ฒด์ ์ข
๋ฅ : ๋ฐฐ์ด ๊ฐ์ฒด
ํ์ค ๋ด์ฅ ๊ฐ์ฒด(Standard Built-in Object)๋ ์๋ฐ์คํฌ๋ฆฝํธ๊ฐ ๊ธฐ๋ณธ์ ์ผ๋ก ๊ฐ์ง๊ณ ์๋ ๊ฐ์ฒด๋ค์ ๋งํ๋ฉฐ, ๋ค๋ฅธ ๊ฐ์ฒด์ ๊ธฐ์ด๊ฐ ๋๋ ํต์ฌ์ ์ธ ๊ฐ์ฒด ์
๋๋ค.
๋ด์ฅ ๊ฐ์ฒด์ ์ข
๋ฅ์๋ Object, Fuction, String, Array, Math, Number, Event, Boolean, Data, RegExp ๋ฑ์ด ์์ต๋๋ค.
๊ทธ ์ค์์ ๋ฐฐ์ด ๊ฐ์ฒด(Array Object)๋ ์ฐ๊ด์ฑ ์๋ ์ฌ๋ฌ ๋ฐ์ดํฐ๋ฅผ ํ๋์ ์ด๋ฆ์ผ๋ก ์ ์ฅ/๊ด๋ฆฌํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ๊ฐ์ฒด๋ฅผ ์๋ฏธํฉ๋๋ค.
___ ์ฐธ๊ณ ํ๊ธฐ ___
1) ๋ฐฐ์ด์ ์์ฑํ ๋๋ ๋๊ดํธ[ ]๋ฅผ ํ์ฉํ ๋ฐฐ์ด ๋ฆฌํฐ๋ด ํ๊ธฐ๋ฒ์ ์ฌ์ฉํ๊ฑฐ๋, Array( )๋ผ๋ ์์ฑ์ ํจ์๋ฅผ ์ฌ์ฉํฉ๋๋ค.
2) ๋ค์ ( )๊ฐ ๋ถ์ง ์๋ '์์ฑ'์, ์๋ฌธ๋ฒ์ ๋น์ ํ์๋ฉด '๋ช
์ฌ'๋ผ๊ณ ํ ์ ์์ต๋๋ค. โ length; ์์ฑ
์์ฑ์ ์ฌ์ฉํ๋ฉด ๋ฐ์ดํฐ๊ฐ ๊ธฐ์กด์ ๊ฐ์ง๊ณ ์๋ ๊ทธ๋๋ก์ ๊ฒ์ ๋ณด์ฌ์ฃผ๊ฒ ๋ฉ๋๋ค.
3) ๋ค์ ( )๊ฐ ๋ถ๋ '๋ฉ์๋'๋, ์๋ฌธ๋ฒ์ ๋น์ ํ์๋ฉด '๋์ฌ'๋ผ๊ณ ํ ์ ์์ต๋๋ค. โ join( ); ๋ฉ์๋
๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฐ์ดํฐ์ ๊ฐ์ด๋ ์์น๊ฐ ๋ณํ๋ ๊ฒ๊ณผ ๊ฐ์ด, ๋ฐ์ดํฐ๊ฐ ์ด๋ ํ ์ก์
์ ์ทจํ๊ฒ ๋ฉ๋๋ค.
02. length ์์ฑ
length ๋ ๋ฐฐ์ด์ ๊ธธ์ด๋ฅผ ๊ตฌํ ์ ์๋ ์์ฑ์ผ๋ก, ๋ฐฐ์ด์ ์ ์ฅ๋ '์์์ ๊ฐ์'๋ฅผ ๋ฐํ(๋ฆฌํด)ํฉ๋๋ค.
| |
| const arrNum = [100, 200, 300, 400, 500]; |
| |
| document.querySelector(".sample01_P1").innerHTML = arrNum.length ; |
| |
| |
| const arrTxt = ['a', 'b', 'c', 'd', 'f']; |
| |
| document.querySelector(".sample01_P2").innerHTML = arrTxt.length ; |
| |
| |
| const arr = [1, 2, ['a', 'b']]; |
| |
| document.querySelector(".sample01_P3").innerHTML = arr.length ; |
๋ฒํธ |
๊ธฐ๋ณธ๊ฐ |
์์ฑ |
๋ฆฌํด๊ฐ |
1 |
[100, 200, 300, 400, 500] |
length |
5 |
2 |
['a', 'b', 'c', 'd', 'f'] |
5 |
3 |
[1, 2, ['a', 'b']] |
3 |
03. join ๋ฉ์๋ ๐คฉ๐คฉ๐คฉ
๋ฐฐ์ด์ ๋ชจ๋ ์์๋ฅผ ๊ฒฐํฉํ์ฌ ํ๋์ ๋ฌธ์์ด๋ก ๋ฐํํฉ๋๋ค.
๋ฐํ๊ฐ์ผ๋ก ๋์ค๋ ๋ฌธ์์ด์ ๊ตฌ๋ถ์(๋์ด์ฐ๊ธฐ, ํน์๋ฌธ์, - ๋ค์, ๋ฑ)์ ์ง์ ํ ์ ์์ต๋๋ค.
| const arrNum = [100, 200, 300, 400, 500]; |
| |
| const text1 = arrNum.join(''); |
| |
| const text2 = arrNum.join(' '); |
| |
| const text3 = arrNum.join('๐ค'); |
| |
| const text4 = arrNum.join('-'); |
๋ฒํธ |
๊ธฐ๋ณธ๊ฐ |
๋ฉ์๋ |
๋ฆฌํด๊ฐ |
1 |
[100, 200, 300, 400, 500] |
join('') |
100200300400500 |
2 |
join(' ') |
100 200 300 400 500 |
3 |
join('๐ค') |
100๐ค200๐ค300๐ค400๐ค500 |
4 |
join('-') |
100-200-300-400-500 |
04. push ๐คฉ๐คฉ/ pop ๋ฉ์๋ ๐ค
push ๋ฉ์๋์ pop ๋ฉ์๋๋ ์๋ณธ ๋ฐฐ์ด์ ์์ ํ๋ ๋ฉ์๋ ์
๋๋ค.
๐ข push( ) ๋ฉ์๋๋ ๋ฐฐ์ด์ ๋ง์ง๋ง์ ์๋ก์ด ์์๋ฅผ ์ถ๊ฐํ ํ, ๋ณ๊ฒฝ๋ ๋ฐฐ์ด์ ๊ธธ์ด(๋ฐฐ์ด์ ์)๋ฅผ ๋ฐํํฉ๋๋ค.
๐ข pop( ) ๋ฉ์๋๋ ๋ฐฐ์ด์ ๋ง์ง๋ง ์์๋ฅผ ์ ๊ฑฐํ ํ, ์ ๊ฑฐํ ์์๋ฅผ ๋ฐํํฉ๋๋ค.
| |
| const arrNum = [100, 200, 300, 400, 500]; |
| const arrPush = arrNum.push(600); |
| |
| document.querySelector(".sample03_N1").innerHTML = "1"; |
| document.querySelector(".sample03_Q1").innerHTML = "[100, 200, 300, 400, 500]"; |
| document.querySelector(".sample03_M1").innerHTML = "push(600)"; |
| document.querySelector(".sample03_P1").innerHTML = arrPush; |
| document.querySelector(".sample03_A1").innerHTML = arrNum; |
| |
| const arrNum2 = [100, 200, 300, 400, 500]; |
| const arrPush2 = arrNum2.pop(); |
| |
| document.querySelector(".sample03_N2").innerHTML = "2"; |
| document.querySelector(".sample03_Q2").innerHTML = "[100, 200, 300, 400, 500]"; |
| document.querySelector(".sample03_M2").innerHTML = "pop()"; |
| document.querySelector(".sample03_P2").innerHTML = arrPush2; |
| document.querySelector(".sample03_A2").innerHTML = arrNum2; |
๋ฒํธ |
๊ธฐ๋ณธ๊ฐ |
๋ฉ์๋ |
๋ฆฌํด๊ฐ |
๊ฒฐ๊ณผ๊ฐ |
1 |
[100, 200, 300, 400, 500] |
push(600) |
6 |
100,200,300,400,500,600 |
2 |
pop( ) |
500 |
100,200,300,400 |
๋๊ธ