Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 코딩테스트
- methoidID
- geth
- Blockchain
- TypeScript
- webpack
- S3
- CA불러오기
- Codestates
- @debug
- scss
- wallet
- 자바스크립트
- 블록체인
- goerli
- HTMLFormElement
- currentTarget
- next.js
- 스마트컨트랙트
- 해쉬테이블
- 다중서명계약
- JavaScript
- Goerlifaucet
- keccak256
- ts-loader
- next-connect
- 자료구조
- Sass
- set-cookie
- incentive
Archives
- Today
- Total
Minwook’s portfolio
배열(Array) 정리 본문
배열의 특징
1. 고정된 크기를 갖는다. 일반적으로는 동적으로 크기를 바꿀 수 없다.
*그러나 자바스크립트에서는 동적으로 배열의 크기가 변한다.
2. 해당원소의 index값을 알고 있으면 O(1)의 시간복잡도로 원소를 찾을 수 있다.
3. 원소를 삭제하면 해당 index의 빈자리가 생긴다.
배열에 요소를 추가, 삭제를 할시 O(n)의 시간이 소요된다
즉, 추가와 삭제가 반복되는 로직에서 배열을 사용하지 않는다.
자바스크립트에서 배열의 특징
1. 동적으로 배열의 크기가 변한다.
2. 배열안에 숫자외에도 문자, Boolean 등이 들어갈 수 있다.
3. 객체로 취급된다. typeof arr === 'object'
배열을 생성하는 방법. 1
const arr1 = [];
const arr2 = [1,2,3];
const arr3 = new Array(3);
const arr4 = new Array(3).fill(0);
console.log(arr1); //[]
console.log(arr2); //[1,2,3]
console.log(arr3); //[empty,empty,empty]
console.log(arr4); //[0,0,0]
배열을 생성하는 방법. 2
ex) Array.from(초기화할 배열, function(v,k){}); //func은 배열의 요소를 순회, v는 value, k는 index값
const arr1 = Array.from(new Array(3), function(v,k){return v=0});
const arr2 = Array.from(new Array(3), (v,k)=>k+1); //화살표함수를 사용
console.log(arr1); //[0,0,0]
console.log(arr2); //[1,2,3]
자주 사용하는 method
pop 맨뒤 요소를 제거
push 맨뒤 요소를 추가
shift 맨앞 요소를 제거
unshift 맨앞 요소를 추가
join 배열의 요소를 합친다.
example arr.join()
const arr = [1,2,3,4,5];
const result1 = arr.join();
const result2 = arr.join(", ");
const result3 = arr.join("");
const result4 = arr.join("and");
console.log(result1); //1,2,3,4,5
console.log(result2); //1, 2, 3, 4, 5
console.log(result3); //12345
console.log(result4); //1and2and3and4and5
reverse 배열의 index를 뒤집는다. 원래 배열에도 영향 가므로 사용시 주의
concat 두 배열을 하나의 배열로 합친다.
slice 배열을 잘라내고 잘라낸 배열을 반환
splice 배열을 잘라내고 남은 배열을 반환(교체하거나 추가도 가능)
'Today I Learned' 카테고리의 다른 글
Heap 정리 (0) | 2022.12.29 |
---|---|
Hashtable 정리 (0) | 2022.12.28 |
시간복잡도, Big O 표기법 (1) | 2022.12.26 |
Express에서 next()의 기능분석 및 에러처리방법 (0) | 2022.10.17 |
node version 관리 및 npm install option (0) | 2022.08.31 |
Comments