Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- autocomplate
- 해외여행
- 마사지
- 삼성무선청소기제트
- Webpack
- 중고나라사기
- 사기
- 세부
- 자동완성
- 막탄
- 스쿠버다이빙
- 구분
- REACT
- 스노쿨링
- js
- 네이버페이사기
- Hooks
- 정규식
- 특수문자
- ES6
- JavaScript
- webpack.config.js
- 여행
- 중고나라
- plugin
- 정직하게사세요
- 맛사지
- 유효성검사
- 중고거래사기
Archives
- Today
- Total
Ryu.log
Array.prototype.splice() 본문
Array.prototype.splice()
Array.prototype.splice() 메서드는 배열내 요소를 삭제, 추가, 변경이 자유롭게 가능하다.
let months = ['Jan', 'March', 'April', 'June']; months.splice(1, 0, 'Feb'); // inserts at 1st index position console.log(months); // expected output: Array ['Jan', 'Feb', 'March', 'April', 'June'] months.splice(4, 1, 'May'); // replaces 1 element at 4th index console.log(months); // expected output: Array ['Jan', 'Feb', 'March', 'April', 'May']
array.splice(시작, 삭제여부, [아이템1, 아이템2, 아이템3 ...]); // 시작 : 배열내 변경할 요소의 인덱스 // 삭제 개수 : 배열에서 제거할 요소의 수, 만약 0개면 아무런 요소도 제거되지 않음 // 아이템N : 추가 및 변경될 요소 만약 선언하지 않으면 splice()는 삭제만 함
반환값
추가, 삭제, 변경된 배열이 리턴 된다. 만약 아무런 요소도 삭제되지 않았을 경우에는 빈 배열이 리턴된다.
예제코드
let myFish = ['angel', 'clown', 'mandarin', 'surgeon']; // removes 0 elements from index 2, and inserts 'drum' let removed = myFish.splice(2, 0, 'drum'); // myFish is ['angel', 'clown', 'drum', 'mandarin', 'surgeon'] // removed is [], no elements removed // removes 1 element from index 3 removed = myFish.splice(3, 1); // myFish is ['angel', 'clown', 'drum', 'surgeon'] // removed is ['mandarin'] // removes 1 element from index 2, and inserts 'trumpet' removed = myFish.splice(2, 1, 'trumpet'); // myFish is ['angel', 'clown', 'trumpet', 'surgeon'] // removed is ['drum'] // removes 2 elements from index 0, and inserts 'parrot', 'anemone' and 'blue' removed = myFish.splice(0, 2, 'parrot', 'anemone', 'blue'); // myFish is ['parrot', 'anemone', 'blue', 'trumpet', 'surgeon'] // removed is ['angel', 'clown'] // removes 2 elements from index 3 removed = myFish.splice(3, Number.MAX_VALUE); // myFish is ['parrot', 'anemone', 'blue'] // removed is ['trumpet', 'surgeon']
'Prev-content' 카테고리의 다른 글
[ JavaScript ] Axios와 async/await을 통한 콜백 지옥 탈출 (0) | 2019.07.25 |
---|---|
[ Webpack ] CRA(create-react-app) Webpack sass-loader 기본 루트 설정 (0) | 2019.01.24 |
Array.prototype.unshift() (0) | 2018.11.13 |
Array.prototype.shift() (0) | 2018.11.13 |
Array.prototype.pop() (0) | 2018.11.13 |
Comments