control structure
자바스크립트에서 배열의 기본적인 활용은 다음과 같다.
1. declaration
//1. declaration
const arr1 = new Array();
const arr2 = [1, 2];
2. Looping
//2. Looping
const fruits = ["🍎", "🍌"];
// a. for
for(let i = 0; i < fruits.length(); i++){
console.log(fruits[i]);
}
// b. for of
for (let fruit of fruits) {
console.log(fruit);
}
// c. forEach
fruits.forEach((fruit, index) => console.log(fruit, index));
3. 추가, 삭제, 복사, 붙이기
// d. Addition, deletion, copy
// push
fruits.push("🍒", "🍑");
console.log(fruits);
// pop
fruits.pop();
console.log(fruits);
//shift: emove an item from the begining
fruits.shift();
console.log(fruits);
//unshitf: add on the item to the begining
fruits.unshift('🍉')
//splice: remove an item by index position
fruits.splice(1,1); // start and end pos
console.log(fruits)
fruits.splice(1,1,'🍏', '🍋')
// combine two arrays
const fruits2 = ['🍇', '🥑'];
const newFruits = fruits.concat(fruits2)
console.log(newFruits);
4. 탐색
//5. Search
console.clear();
console.log(fruits);
console.log(fruits.indexOf('🍉'))
//includes
console.log(fruits.includes('🍏')); // true
//lastIndexof
fruits.push('🍏');
console.log(fruits);
console.log(fruits.indexOf('🍏'));
console.log(fruits.lastIndexOf('🍏'))
출처: YOUTUBE 드림코딩
'javascript' 카테고리의 다른 글
| [함수형 언어, 자바스크립트] 비동기 처리 (2) Promise의 이해 (0) | 2020.11.17 |
|---|---|
| [함수형 언어, 자바스크립트] 비동기 처리 (1) Callback 함수의 이해 (0) | 2020.11.17 |
| [함수형 언어, 자바스크립트] 배열에 함수 체이닝 (1) (0) | 2020.10.22 |
| 함수형 언어, 자바스크립트 (0) | 2020.10.14 |
| 함수형 언어, 자바스크립트 START (0) | 2020.10.14 |