💡
JavaScript === dynamically typed language → 여러 타입을 담을 수 가 있다!! ! 검색, 정렬, 삽입, 삭제 !!! (array map set list) 자료구조, 알고리즘을 써야 좋은지를 확인해야 한다!!! 동일한 타입의 배열을 넣는 것이 좋다!! 삭제도 가능하다!!
'use strict'
// 1. Declaration
const arr1 = new Array();
const arr2 = [1,2];
//2. Index position
const fruits = ['apple', 'banana'];
console.log(fruits);
console.log(fruits.length);
console.log(fruits[0]);
console.log(fruits[1]);
console.log(fruits[fruits.length-1]);
console.log(fruits[-1]); //에러
console.clear();
// 3. Loopgin over an array
// print all fruits
// a. for
for (let i = 0; i<fruits.length; i++) {
console.log(fruits[i]);
}
// b. for
for (let fruit of fruits) {
console.log(fruit);
}
//c.for
fruits.forEach(function(fruit, index, array){
console.log(fruit, index);
});//callback 함수를 불러옴!!
//c-1.forEach
fruits.forEach((fruit) => console.log(fruit));
//callback 함수를 불러옴!!
//4.Addition, deletion, copy
// add an item to the end
fruits.push('apple','orange');
console.log(fruits);// apple, banana, apple, orange
// pop : remove an item from the end
fruits.pop();
console.log(fruits); //apple, banana, apple
// unshift : add an item to the beginning
// shift : remove an item from the beginning
fruits.unshift('strawberry','lemon');
console.log(fruits); //strawberry, lemon, apple, banana, apple
fruits.shift();
fruits.shift();
console.log(fruits); //apple, banana, apple
//note!! shift, unshift are slower than pop, push -> stack이라 빼고 다시 넣어야함!
//splice : remove an item by index position
console.clear();
const fruits2 = ['apple','strawbery','orange','lemon','banana','grape'];
console.log(fruits2);
//fruits2.splice(2); //0번째 부터 n개만 남음
//fruits2.splice(1,3); //1번쨰 인덱스부터 3개 삭제
fruits2.splice(1,1,'kiwi','peach');//1번째 인덱스 1개 삭제하고, 그자리에 삽입!
console.log(fruits2);
fruits2.splice(4);
//combine two arrays
const fruits3 = ['watermelon','mango'];
const newFruits = fruits2.concat(fruits3);
console.log(newFruits); //합쳐짐
//5. Searching
// find the index
console.clear();
console.log(fruits2);
console.log(fruits2.indexOf('mango')); //없음
console.log(fruits2.indexOf('peach')); //2번쨰
console.log(fruits2.includes('mango')); //false
console.log(fruits2.includes('kiwi')); //true
//last
console.clear();
fruits2.push('apple');
console.log(fruits2);
console.log(fruits2.indexOf('apple')); //0
console.log(fruits2.lastIndexOf('apple')); //4 -> 뒤에서 찾는 거!!
Notion2Tistory
boltlessengineer.github.io
반응형
'Develope_Web_Programming > 01_HTML&CSS&JS' 카테고리의 다른 글
CSS 선택자 (0) | 2021.02.14 |
---|---|
9. 유용한 10가지 배열 함수들 (0) | 2021.02.14 |
7.오브젝트 넌 뭐니?? (0) | 2021.02.14 |
6. 클래스와 오브젝트의 차이 (0) | 2021.02.13 |
5. Arrow Function은 무엇인가? (0) | 2021.02.13 |
Uploaded by Notion2Tistory v1.1.0