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 | 31 |
Tags
- express-handlebars
- Express
- es6
- 디자인패턴
- flex-grow
- flexbox
- node
- regExp
- 정규표현식
- shit-christmas
- Object.create
- css variables
- 무료 백엔드 배포
- flex-shrink
- close together
- ajax
- css
- css grid
- css 오버레이
- module wrapper function
- improve-iq-by-up-to-10%!
- select by attribute
- flex-basis
- Prototype
- just-one-small-sip
- Engoo
- Sass
- JS
- img 확대
- Node.js
Archives
- Today
- Total
Arrays 본문
Arrays - variable that hold multiple values 여러개의 값을 가질 수 있는 변수
new 키워드 다음에 Array() 라는 constructor를 이용해 배열을 생성할 수 있다
const numbers = new Array(1,2,3,4,5);
다음과 같이 생성할 수도 있다
const fruits = ['apple', 'banana', 'strawberry'];
자바스크립트에서는 하나의 배열에 여러가지 데이터타입을 담을 수도 있다
const fruits = ['apple', 'banana', 10, false];
이런 동적인 부분을 정적으로 ( ex) const number:string ) 표현하려면 typescript라는 것을 이용할 수 있다고 한다.
배열은 zero-based 이기 때문에 0부터 접근 가능하다.
const fruits = ['apple', 'banana', 10, false];
console.log(fruits[0]); // apple
console.log(fruits[2]); // 10
const를 이용해 배열을 선언해도 다음과 같이 배열을 동적으로 늘릴 수 있다.
const fruits = ['apple', 'banana', 10, false];
fruits[4] = 'grapes';
console.log(fruits[4]); // grapes
다만 재할당을 시도하는 경우 에러가 발생한다.

위 처럼 동적으로 늘리는 방식도 있지만 push 메서드를 이용해 배열의 맨 마지막에 추가하는 방법도 있다.

아래처럼 배열의 맨 처음에 추가하는 방법도 있다.
fruits.unshift('strawberry');
맨 마지막의 값을 제거할 수도 있다.
fruits.pop();
다음과 같이 인자로 들어온 것이 배열인지 체크도 가능하다.
Array.isArray(fruits)
배열 순서를 역으로 바꿀 수 있다.
fruits.reverse();
정렬도 가능
// sort with compare function
// ascending
fruits.sort((x, y) => {
return x - y;
});
// descending
fruits.sort((x, y) => {
return y - x;
});
'2021 프론트 엔드 로드맵 따라가기 > JS' 카테고리의 다른 글
JSON (0) | 2021.05.27 |
---|---|
Object literal (0) | 2021.05.27 |
String (0) | 2021.05.27 |
primitive types & referece types (0) | 2021.05.27 |
변수 선언 방식 (0) | 2021.05.27 |