Post

자바스크립트 - 배열 객체 01

안녕하세요 오늘은 자바스크립트 배열 객체 매서드에 대해서 알아보려고 합니다.

배열 객체에 대해서 33개정도 정리할건데 오늘은 16개 정리 해보겠습니다.

이전의 문자열 객체랑 겹치는게 몇개 있을 수 있습니다.

자바스크립트 배열 객체 01

1. at() 메서드:

  • 설명: 문자열에서 지정한 인덱스의 요소를 반환합니다.
  • 사용 예시:
    1
    2
    
    const str = 'Hello';
    console.log(str.at(1)); // 'e'
    

2. charAt() 메서드:

  • 설명: 문자열에서 지정한 인덱스의 단일문자를 반환합니다.
  • 사용 예시:
    1
    2
    
    const str = 'Hello';
    console.log(str.charAt(2)); // 'l'
    

3. charCodeAt() 메서드:

  • 설명: 문자열에서 지정한 인덱스의 유니코드 정수 값을 반환합니다.
  • 사용 예시:
    1
    2
    
    const str = 'Hello';
    console.log(str.charCodeAt(0)); // 72
    

4. concat() 메서드:

  • 설명: 현재 배열에 다른 배열이나 값들을 합쳐서 새로운 배열을 반환합니다.
  • 사용 예시:
    1
    2
    3
    4
    
    const array1 = ['a', 'b', 'c'];
    const array2 = ['d', 'e', 'f'];
    const newArray = array1.concat(array2);
    console.log(newArray); // ['a', 'b', 'c', 'd', 'e', 'f']
    

5. copyWithin() 메서드:

  • 설명: 배열의 일부분을 복사하여 동일한 배열의 다른 위치에 덮어씁니다.
  • 사용 예시:
    1
    2
    3
    
    const array = ['a', 'b', 'c', 'd', 'e'];
    array.copyWithin(0, 3, 4);
    console.log(array); // ['d', 'b', 'c', 'd', 'e']
    

6. entries() 메서드:

  • 설명: 배열의 각 인덱스에 대한 키/값 쌍을 포함하는 새로운 배열 반복자 객체를 반환합니다.
  • 사용 예시: ```javascript const array = [‘a’, ‘b’, ‘c’]; const iterator = array.entries();

for (const [index, value] of iterator) { console.log(index, value); } // 0 ‘a’ // 1 ‘b’ // 2 ‘c’

1
2
3
4
5
6
7
8
## 7. **every() 메서드**:
   - 설명: 배열의 모든 요소가 주어진 조건을 만족하는지를 확인하여 불린 값을 반환합니다.
   - 사용 예시:
```javascript
const array = [1, 30, 39, 29, 10, 13];
const isBelowThreshold = (currentValue) => currentValue < 40;
console.log(array.every(isBelowThreshold)); // true

8. fill() 메서드:

  • 설명: 배열의 시작 인덱스부터 끝 인덱스까지 정적 값 하나로 채웁니다.
  • 사용 예시:
    1
    2
    
    const array = [1, 2, 3, 4, 5];
    console.log(array.fill(0, 2, 4)); // [1, 2, 0, 0, 5]
    

9. filter() 메서드:

  • 설명: 주어진 함수로 구현된 테스트를 통과하는 모든 요소를 담은 새로운 배열을 생성합니다.
  • 사용 예시:
    1
    2
    3
    
    const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
    const result = words.filter(word => word.length > 6);
    console.log(result); // ['exuberant', 'destruction', 'present']
    

10. find() 메서드:

  • 설명: 주어진 판별 함수를 만족하는 배열의 첫 번째 요소의 값을 반환합니다.
  • 사용 예시:
    1
    2
    3
    
    const array = [5, 12, 8, 130, 44];
    const found = array.find(element => element > 10);
    console.log(found); // 12
    

11. findIndex() 메서드:

  • 설명: 주어진 판별 함수를 만족하는 배열의 첫 번째 요소의 인덱스를 반환합니다.
  • 사용 예시:
    1
    2
    3
    
    const array = [5, 12, 8, 130, 44];
    const foundIndex = array.findIndex(element => element > 10);
    console.log(foundIndex); // 1
    

12. flat() 메서드:

  • 설명: 모든 하위 배열 요소를 지정된 깊이까지 재귀적으로 병합한 새로운 배열을 생성합니다.
  • 사용 예시:
    1
    2
    
    const arr1 = [0, 1, 2, [3, 4]];
    console.log(arr1.flat()); // [0, 1, 2, 3, 4]
    

13. flatMap() 메서드:

  • 설명: 먼저 매핑 함수를 사용하여 각 요소에 대해 map을 호출한 후, 하나의 결과 배열을 평탄화합니다.
  • 사용 예시:
    1
    2
    
    const arr1 = [1, 2, 3, 4];
    console.log(arr1.flatMap(x => [x * 2])); // [2, 4, 6, 8]
    

14. forEach() 메서드:

  • 설명: 배열의 각 요소에 대해 제공된 함수를 실행합니다.
  • 사용 예시:
    1
    2
    3
    4
    5
    6
    
    const array1 = ['a', 'b', 'c'];
    array1.forEach(element => console.log(element));
    // 예상 출력:
    // 'a'
    // 'b'
    // 'c'
    

15. includes() 메서드:

  • 설명: 배열에 특정 요소가 포함되어 있는지를 확인하여 불린 값을 반환합니다.
  • 사용 예시:
    1
    2
    
    const array1 = [1, 2, 3];
    console.log(array1.includes(2)); // true
    

16. indexOf() 메서드:

  • 설명: 배열에서 지정된 요소를 찾아 첫 번째 인덱스를 반환하고 존재하지 않으면 -1을 반환합니다.
  • 사용 예시:
    1
    2
    
    const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
    console.log(beasts.indexOf('bison')); // 1
    

이렇게 오늘 16개 정리를 해봤습니다.

나머지 17개는 다음글에 정리하도록 하겠습니다.

오늘도 필자의 부족한 글 읽어주셔서 감사합니다.

This post is licensed under CC BY 4.0 by the author.