JavaScript / Object / Array.push(), Array.pop(), Array.unshift(), Array.shift() - 배열에 원소 추가/제거 하기
.push(), .pop(), unshift(), shift()
- .push()는 배열의 맨 끝에, .unshift()는 배열의 맨 앞에 원소를 추가합니다.
- .pop()은 마지막 원소를, .shift()는 맨 앞의 원소를 제거합니다.
예제
네 가지 속성을 비교하는 간단한 예제입니다.
<!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>JavaScript</title> </head> <body> <script> var jbAry1 = [ 'one', 'two', 'three' ]; jbAry1.push( 'four' ); document.write( '<p>' + jbAry1.join( ' / ' ) + '</p>' ); var jbAry2 = [ 'one', 'two', 'three' ]; jbAry2.pop(); document.write( '<p>' + jbAry2.join( ' / ' ) + '</p>' ); var jbAry3 = [ 'one', 'two', 'three' ]; jbAry3.unshift( 'zero' ); document.write( '<p>' + jbAry3.join( ' / ' ) + '</p>' ); var jbAry4 = [ 'one', 'two', 'three' ]; jbAry4.shift(); document.write( '<p>' + jbAry4.join( ' / ' ) + '</p>' ); </script> </body> </html>