λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
Javascript

μžλ°”μŠ€ν¬λ¦½νŠΈ : λ°°μ—΄ λ©”μ„œλ“œ : join(), push(), pop()

by μ½”νŒŒμΉ΄ 2022. 8. 11.
728x90

λ°°μ—΄ λ©”μ„œλ“œ : join(), push(), pop()

배열은 λ¦¬μŠ€νŠΈμ™€ λΉ„μŠ·ν•œ κ°μ²΄λ‘œμ„œ μˆœνšŒμ™€ λ³€ν˜• μž‘μ—…μ„ μˆ˜ν–‰ν•˜λŠ” λ©”μ„œλ“œλ₯Ό κ°–μŠ΅λ‹ˆλ‹€. 배열은 길이, μš”μ†Œμ˜ μžλ£Œν˜•μ΄ κ³ μ •λ˜μ–΄μžˆμ§€ μ•ŠκΈ° λ•Œλ¬Έμ—, 길이가 μ–Έμ œλ“ μ§€ λŠ˜μ–΄λ‚˜κ±°λ‚˜ 쀄어듀 수 있고, 데이터λ₯Ό 연속적이지 μ•Šμ€ 곳에 μ €μž₯ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μ΄λŸ¬ν•œ 배열을 νŽΈλ¦¬ν•˜κ²Œ 닀루기 μœ„ν•΄ μ‚¬μš©λ˜λŠ” λ©”μ„œλ“œμ— λŒ€ν•˜μ—¬ μ•Œμ•„λ³΄κ² μŠ΅λ‹ˆλ‹€.


join() λ©”μ„œλ“œ

join()은 λ°°μ—΄ μš”μ†Œλ“€μ„ λ¬Έμžμ—΄λ‘œ κ²°ν•©ν•΄ μ£Όμ–΄, λ¬Έμžμ—΄λ‘œ λ°˜ν™˜ν•˜λŠ” λ©”μ„œλ“œμž…λ‹ˆλ‹€.

const arrNum = [100, 200, 300, 400, 500];
const text1 = arrNum.join('')
const text2 = arrNum.join(' ')
const text3 = arrNum.join('β˜…')
const text4 = arrNum.join('-')

document.write(text1)
document.write(text2)
document.write(text3)
document.write(text4)
결과보기
100200300400500
100 200 300 400 500
100β˜…200β˜…300β˜…400β˜…500
100-200-300-400-500

push() λ©”μ„œλ“œ

push()λŠ” λ°°μ—΄μ˜ 끝에 ν•˜λ‚˜ μ΄μƒμ˜ μš”μ†Œλ₯Ό μΆ”κ°€ν•˜κ³ , λ°°μ—΄μ˜ μƒˆλ‘œμš΄ 길이값을 숫자둜 λ°˜ν™˜ν•˜λŠ” λ©”μ„œλ“œμž…λ‹ˆλ‹€.

const arrNum = [100, 200, 300, 400, 500];
const arrPush = arrNum.push(600);  // λ§ˆμ§€λ§‰ μš”μ†Œμ— 600을 μΆ”κ°€

document.write(arrPush)  // λ°°μ—΄μ˜ 길이값(숫자) 좜λ ₯
document.write(arrNum)  // λ°°μ—΄ μš”μ†Œλ“€ 좜λ ₯
결과보기
6
100, 200, 300, 400, 500, 600

pop() λ©”μ„œλ“œ

pop()은 λ°°μ—΄μ˜ λ§ˆμ§€λ§‰ μš”μ†Œλ₯Ό μ‚­μ œν•˜κ³ , μ‚­μ œλœ μš”μ†Œλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

const arrNum = [100, 200, 300, 400, 500];
const arrPop = arrNum.pop();  // λ§ˆμ§€λ§‰ μš”μ†Œλ₯Ό μ‚­μ œ

document.write(arrPop);  // μ‚­μ œλœ μš”μ†Œ 좜λ ₯
document.write(arrNum);  // λ‚˜λ¨Έμ§€ μš”μ†Œλ“€ 좜λ ₯
결과보기
500
100,200,300,400

λ©”μ„œλ“œ 정리

각 λ©”μ„œλ“œλ“€μ„ μ •λ¦¬ν•˜λ©΄ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

λ©”μ„œλ“œ μˆ˜ν–‰ μž‘μ—… λ°˜ν™˜κ°’
join() λ°°μ—΄ μš”μ†Œ λ¬Έμžμ—΄ κ²°ν•© λ¬Έμžμ—΄
push() λ°°μ—΄ λ§ˆμ§€λ§‰ μš”μ†Œμ— μΆ”κ°€ 숫자(길이값)
pop() λ°°μ—΄ λ§ˆμ§€λ§‰ μš”μ†Œλ₯Ό μ‚­μ œ μ‚­μ œλœ μš”μ†Œ
728x90

λŒ“κΈ€

κ°μ‚¬ν•©λ‹ˆλ‹€. πŸ¦™

CSS
κ΄‘κ³  쀀비쀑