티스토리 뷰
1. Decalaration
배열을 만드는 두 가지 방법
const arr1 = new Array();
const arr1 = [1, 2];
2. Index position
배열은 인덱스 값이 1 2 3이 아닌 0 1 2로 된다는 걸 명심!
const fruits = ['a','b'];
console.log(fruits); // a, b 둘다 출력됨
console.log(fruits.length); // 배열의 크기인 2가 출력됨
console.log(fruits[0]); // 배열의 첫번째인 a가 출력됨
console.log(fruits[1]); // 배열의 두번째인 b가 출력됨
console.log(fruits[2]); // undefined가 출력됨(이는 인덱스2는 배열에 없기 때문)
console.log(fruits[fruits.length - 1]); // 마지막 데이터는 이런식으로 접근함
3. Looping over an array
과일 배열을 출력하는 3가지 방식이다.
foreach는 괄호 안에 각각의 배열마다 액션을 취하게 해주는 함수이다.// print all 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) => console.log(fruit));
4. addtion, deletion, copy
// push : add an item to the end
fruits.push('a','b');
console.log(fruits);
//pop : remove an item from the end
fruits.pop();
fruits.pop();
console.log(fruits);
//unshift : add an item to the benigging
fruits.unshift('c','d');
console.log(fruits);
//shift : remove an item from the benigging
fruits.shift();
fruits.shift();
console.log(fruits);
// shift unshift는 pop과 push보다 매우 느리니 사용 자제
// splice : remove an item by index position
fruits.push('a','b','c');
console.log(fruits);
fruits.splice(1,1); // 1부터 1까지 즉 인데스가 1인 위치를 없앰
console.log(fruits);
fruits.splice(1,1,'a','b'); // 인덱스가 1인 곳을 없애고 a랑 b를 집어넣음
console.log(fruits);
// combine two arrays
const fruits2 = ['f','e'];
const newFruits = fruits.concat(fruits2);
console.log(newFruits);
shift unshift는 2번째 데이터를 3번째에 넣고 1번째 데이터를 2번째에 넣고 0번째에 새로운 데이터를 집어넣어야 되는
과정 속에서 시간을 잡아먹는다.
fruits.splice(1);
이런식으로 splice를 쓰게 되면 우리가 지정한 인덱스를 포함하여 뒷부분을 모두 지워버린다.
그래서 위처럼 지정해줘서 사용한다. 또한 지우고 채워 넣는 것도 가능하다.
const newFruits = fruits.concat(fruits2);
. concat(배열)을 이용하여 두 배열을 합칠 수 있다.
5. searching
아래와 같이 유용하게 검색이 가능하다
// index of : find the index
console.clear();
console.log(fruits);
console.log(fruits.indexOf('a')); // 인덱스 위치를 알려줌
console.log(fruits.indexOf('b'));
console.log(fruits.indexOf('q')); //만약 없으면 -1을 출력
// includes
console.log(fruits.includes('b')); // 있으면 true
console.log(fruits.includes('q')); // 없으면 false
// lastIndexOf
fruits.push('a');
console.log(fruits.indexOf('a')); //맨 처음 사과의 인덱스 출력
console.log(fruits.lastIndexOf('a')); //맨 마지막 사과의 인덱스 출력
만약 라이브러리 내용이 궁금하다면 윈도우는 ctrl을 누른 뒤 클릭하면
* Returns the index of the last occurrence of a specified value in an array, or -1 if it is not present.
* @param searchElement The value to locate in the array.
* @param fromIndex The array index at which to begin searching backward. If fromIndex is omitted, the search starts at the last index in the array.
*/
lastIndexOf(searchElement: T, fromIndex?: number): number;
다음과 같이 친절한 설명이 나온다
'웹프로그래밍 > js' 카테고리의 다른 글
유용한 10가지 배열 함수들. Array APIs 총정리(드림코딩 by 엘리님 유튜브 강의) (0) | 2022.03.10 |
---|---|
오브젝트 넌 뭐니?(드림코딩 by 엘리님 유튜브 강의) (0) | 2022.03.09 |
arrow function 은 무엇인가? 함수의 선언과 표현(드림코딩 by 엘리님 유튜브 강의) (0) | 2022.03.09 |
코딩의 기본 operator, if, for loop 코드리뷰 팁(드림코딩 by 엘리님 유튜브 강의) (0) | 2022.03.08 |
데이터타입 let vs var, hoisting(드림코딩 by 엘리님 유튜브 강의) (0) | 2022.03.08 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 2단레이아웃
- 다단레이아웃
- 웹 코딩
- 부동산
- 레이아웃작성
- 웹프로그래밍
- 홈페이지 메뉴 만들기
- 고정 레이아웃
- htmlcss 매뉴만들기
- 경매공부
- 부스트보스
- 경매
- 이해강
- 고정레이아웃
- 레이아웃제작
- 부동산스터디
- 1단 메뉴 제작
- 메뉴만들기
- 프로모션 페이지 제작
- 1단 메뉴만들기
- 프론트공부
- CSS
- 네이버 부스트코스
- HTML
- 부동산경매
- 고정레이아웃제작
- 고정 레이아웃 제작
- float효과삭제
- float효과제거하기
- 1단레이아웃제작
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함