본문 바로가기
Programming/JavaScript

[JavaScript] Arrays (배열)

by inyeong 2024. 9. 25.

1. Arrays (배열)

배열(Array)은 자바스크립트에서 리스트를 만드는 방법이다. 

배열에는 모든 데이터 타입을 저장할 수 있고, 서로 다른 데이터 타입을 가진 배열도 만들 수 있다. 

 let concepts = ['creating arrays', 'array structures', 'array manipulation'];

 

2. Create an Array (배열 생성) 

항목들을 [ ] 로 감싸서 배열을 생성하고, 각 항목을 요소(element)라 한다. 

배열을 변수에 저장할 수도 있다. 배열을 사용하면 여러 데이터를 하나의 변수에 모아 관리할 수 있다.

let favoriteFoods = ['Pizza', 'Sushi', 'Chocolate', 'Pasta', 42, true];

 

3. Accessing Elements (배열 요소 접근)

배열은 순서가 있는 데이터 구조로, 각 항목은 번호가 매겨진 위치인 인덱스(index)를 가진다.

배열의 인덱스를 사용하여 개별 항목에 접근할 수 있다. 첫 번째 항목은 인덱스 0에 위치하게 된다.

 

ex) myArray[0]

const hello = 'Hello World';
console.log(hello[6]);
// Output: W

 

4. Update Elements (배열 요소 수정)

배열의 요소에 접근하여 값을 수정할 수도 있다.

 

ex) seasons[3] = 'Autumn';

let seasons = ['Winter', 'Spring', 'Summer', 'Fall'];

seasons[3] = 'Autumn';
console.log(seasons);
//Output: ['Winter', 'Spring', 'Summer', 'Autumn']

 

5. let과 const 

const로 선언된 배열의 요소도 변경할 수 있다.

const 배열의 내용은 수정할 수 있지만, 그 배열 자체를 다른 배열이나 값으로 재할당할 수는 없다.

 

6. Array Property 

배열의 내장 속성 중 하나인 length는 배열의 항목 수를 반환한다.

const newYearsResolutions = ['Keep a journal', 'Take a falconry class'];

console.log(newYearsResolutions.length);
// Output: 2

 

7. Array Method 

  1. .push( ) : 배열의 끝에 항목 추가 (원본 배열 변경)
    const itemTracker = ['item 0', 'item 1', 'item 2'];
    
    itemTracker.push('item 3', 'item 4');
    
    console.log(itemTracker);
    // Output: ['item 0', 'item 1', 'item 2', 'item 3', 'item 4'];
  2. .pop( ) : 배열의 마지막 요소를 제거  (원본 배열 변경)
    const newItemTracker = ['item 0', 'item 1', 'item 2'];
    
    const removed = newItemTracker.pop();
    
    console.log(newItemTracker);
    // Output: [ 'item 0', 'item 1' ]
    console.log(removed);
    // Output: item 2

  3. .join(), .slice(), .splice().shift().unshift(), and .concat()

 

8. Array and Functions (배열과 함수)

함수 내부에서 배열이 변형되면 그 변경 사항은 함수 외부에도 유지된다. 

함수에 전달하는 것은 변수 메모리가 저장된 위치에 대한 참조, 이는 메모리를 변경하는 것이기 때문이다.

const flowers = ['peony', 'daffodil', 'marigold'];

function addFlower(arr) {
  arr.push('lily');
}

addFlower(flowers);

console.log(flowers); // Output: ['peony', 'daffodil', 'marigold', 'lily']

 

9. Nested Array  (중첩 배열)

배열이 다른 배열을 포함하고 있을 때, 이를 nested array(중첩 배열)이라 한다. 
ex) const nestedArr = [[1], [2, 3]];

 

중첩 배열 요소 접근 
ex) console.log(nestedArr[1]) // [2,3]     console.log(nestedArr[1][0]) // 2 

 


 

예제 코드 : https://github.com/inyeongjang/Snoopy/blob/main/week02/5-Arrays.js

 

Snoopy/week02/5-Arrays.js at main · inyeongjang/Snoopy

Contribute to inyeongjang/Snoopy development by creating an account on GitHub.

github.com

 

참고자료 : https://www.codecademy.com/enrolled/courses/introduction-to-javascript

 

Learn JavaScript | Codecademy

Begin your web development journey with our JavaScript course. Explore dynamic scripting for interactive web solutions.

www.codecademy.com

 

'Programming > JavaScript' 카테고리의 다른 글

[JavaScript] Objects (객체)  (0) 2024.09.29
[JavaScript] Loops & Iterators  (0) 2024.09.28
[JavaScript] Scope  (0) 2024.09.25
[JavaScript] Functions (함수)  (0) 2024.09.25
[JavaScript] Conditional Statements (조건문)  (0) 2024.09.25