티스토리 뷰
반응형
Array
Declaration
const arr1 = new Array(); const arr2 = [1, 2];
Index position
const fruits = ['apple', 'banana']; console.log(fruits[1]); // banana console.log(fruits[2]); // undefined
Looping over an array
for 사용
for (let i = 0; i < fruits.length ; i++) { console.log(fruits[i]); }
for ... of 사용
for (let fruit of fruits) { console.log(fruit); }
forEach 사용
fruits.forEach(function (fruit, index, array) { console.log(fruit, index, array); }); // apple 0 ["apple", "banana"] // banana 1 ["apple", "banana"]
Addition, deletion, copy
push: add an item to the end
pop: remove an item from the end
fruits.push("peach"); console.log(fruits); // ["apple", "banana", "peach"] fruits.pop(); console.log(fruits); // ["apple", "banana"]
unshift: add an item to the beginning
shift: remove an item from the beginning
fruits.unshift("peach"); console.log(fruits); // ["peach", "apple", "banana"] fruits.shift(); console.log(fruits); // ["apple", "banana"]
shift, unshift는 push, pop에 비해 매우 느림
splice: remove an item by index position
fruits.push("peach", "grape"); console.log(fruits); // ["apple", "banana", "peach", "grape"] fruits.splice(2); console.log(fruits); // ["apple", "banana"] fruits.push("peach", "grape"); console.log(fruits); // ["apple", "banana", "peach", "grape"] fruits.splice(2, 1); console.log(fruits); // ["apple", "banana", "grape"] fruits.splice(2, 1, "pear", "orange"); console.log(fruits); // ["apple", "banana", "pear", "orange"]
concat: combine two arrays
const fruits2 = ["peach", "grape"]; const newFruits = fruits.concat(fruits2); console.log(newFruits); // ["apple", "banana", "pear", "orange", "peach", "grape"]
Searching
console.log(fruits); // ["apple", "banana", "pear", "orange"] console.log(fruits.indexOf("apple")); // 0 console.log(fruits.indexOf("coconut")); // -1 console.log(fruits.includes("orange")); // true console.log(fruits.includes("coconut")); // false fruits.push("banana"); console.log(fruits); // ["apple", "banana", "pear", "orange", "banana"] console.log(fruits.indexOf("banana")); // 1 console.log(fruits.lastIndexOf("banana")); // 4
반응형
'Javascript' 카테고리의 다른 글
[드림코딩 by 엘리] 9. 유용한 10가지 배열 함수들. Array APIs 총정리 (0) | 2021.09.13 |
---|---|
[드림코딩 by 엘리] 7. 오브젝트 넌 뭐니? (0) | 2021.09.13 |
[드림코딩 by 엘리] 6. 클래스와 오브젝트의 차이점(class vs object), 객체지향 언어 클래스 정리 (0) | 2021.09.13 |
[드림코딩 by 엘리] 5. Arrow Function은 무엇인가? 함수의 선언과 표현 (0) | 2021.09.13 |
[모던 자바스크립트 입문] 8. 함수 (0) | 2021.09.13 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- codeforces
- ft_printf
- printf
- ES6
- 자바스크립트
- ft_server
- 부스트코딩뉴비챌린지
- Python
- 42cursus
- 42서울
- 알고리즘
- 부스트코스
- 코딩뉴비챌린지
- BOJ
- 코린이
- 네이버커넥트재단
- django
- 멋쟁이사자처럼9기
- 컴퓨터과학
- C++
- 멋쟁이사자처럼
- 42seoul
- 드림코딩by엘리
- github
- 드림코딩
- CS50
- 코린이의 성장일기
- git
- 코드포스
- 백준
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 | 31 |
글 보관함