Post

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

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

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

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

자바스크립트 배열 객체 02

17. join() 메서드:

  • 설명: 배열의 모든 요소를 문자열로 결합하여 반환합니다.
  • 사용 예시:
    1
    2
    
    const elements = ['Fire', 'Wind', 'Rain'];
    console.log(elements.join()); // "Fire,Wind,Rain"
    

18. keys() 메서드:

  • 설명: 배열의 각 인덱스에 대한 키 값들을 가지는 새로운 Array Iterator 객체를 반환합니다.
  • 사용 예시: ```javascript const array1 = [‘a’, ‘b’, ‘c’]; const iterator = array1.keys();

for (const key of iterator) { console.log(key); // 0, 1, 2 }

1
2
3
4
5
6
7
## 19. **lastIndexOf() 메서드**:
   - 설명: 배열에서 지정된 요소를 찾아 마지막 인덱스를 반환하고 존재하지 않으면 -1을 반환합니다.
   - 사용 예시:
```javascript
const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
console.log(beasts.lastIndexOf('bison')); // 4

20. map() 메서드:

  • 설명: 배열 내의 모든 요소에 대해 제공된 함수를 호출한 결과를 모아서 새로운 배열을 반환합니다.
  • 사용 예시:
    1
    2
    3
    
    const numbers = [1, 4, 9];
    const roots = numbers.map(Math.sqrt);
    console.log(roots); // [1, 2, 3]
    

21. pop() 메서드:

  • 설명: 배열에서 마지막 요소를 제거하고 해당 요소를 반환합니다.
  • 사용 예시:
    1
    2
    3
    
    const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
    console.log(plants.pop()); // "tomato"
    console.log(plants); // ['broccoli', 'cauliflower', 'cabbage', 'kale']
    

22. push() 메서드:

  • 설명: 배열의 끝에 하나 이상의 요소를 추가하고, 배열의 새로운 길이를 반환합니다.
  • 사용 예시:
    1
    2
    3
    4
    
    const animals = ['pigs', 'goats', 'sheep'];
    const count = animals.push('cows');
    console.log(count); // 4
    console.log(animals); // ['pigs', 'goats', 'sheep', 'cows']
    

23. reduce() 메서드:

  • 설명: 배열의 각 요소에 대해 주어진 리듀서 함수를 실행하고 하나의 결과값을 반환합니다.
  • 사용 예시:
    1
    2
    3
    
    const array1 = [1, 2, 3, 4];
    const reducer = (accumulator, currentValue) => accumulator + currentValue;
    console.log(array1.reduce(reducer)); // 10
    

24. reduceRight() 메서드:

  • 설명: 배열의 각 요소에 대해 오른쪽에서 왼쪽으로 리듀서 함수를 실행하고 하나의 결과값을 반환합니다.
  • 사용 예시:
    1
    2
    3
    4
    
    const array1 = [[0, 1], [2, 3], [4, 5]].reduceRight(
      (accumulator, currentValue) => accumulator.concat(currentValue)
    );
    console.log(array1); // [4, 5, 2, 3, 0, 1]
    

25. reverse() 메서드:

  • 설명: 배열의 순서를 반대로 바꾸고 해당 배열을 반환합니다.
  • 사용 예시:
    1
    2
    
    const array1 = ['one', 'two', 'three'];
    console.log(array1.reverse()); // ['three', 'two', 'one']
    

26. shift() 메서드:

  • 설명: 배열에서 첫 번째 요소를 제거하고 해당 요소를 반환합니다.
  • 사용 예시: ```javascript const array1 = [1

, 2, 3]; const firstElement = array1.shift(); console.log(array1); // [2, 3] console.log(firstElement); // 1

1
2
3
4
5
6
7
8
## 27. **unshift() 메서드**:
   - 설명: 배열의 맨 앞에 하나 이상의 요소를 추가하고, 새로운 길이를 반환합니다.
   - 사용 예시:
```javascript
const array1 = [1, 2, 3];
console.log(array1.unshift(4, 5)); // 5
console.log(array1); // [4, 5, 1, 2, 3]

28. slice() 메서드:

  • 설명: 배열의 일부분에 대한 얇은 복사본을 새 배열 객체로 반환합니다.
  • 사용 예시:
    1
    2
    3
    
    const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
    console.log(animals.slice(2)); // ["camel", "duck", "elephant"]
    console.log(animals.slice(2, 4)); // ["camel", "duck"]
    

29. splice() 메서드:

  • 설명: 배열의 내용을 삭제 또는 교체하거나 새 요소를 추가하여 배열의 내용을 변경합니다.
  • 사용 예시:
    1
    2
    3
    
    const months = ['Jan', 'March', 'April', 'June'];
    months.splice(1, 0, 'Feb');
    console.log(months); // ['Jan', 'Feb', 'March', 'April', 'June']
    

30. sort() 메서드:

  • 설명: 배열의 요소를 적절한 위치에 정렬하고 그 배열을 반환합니다.
  • 사용 예시:
    1
    2
    
    const array1 = [1, 30, 4, 21];
    console.log(array1.sort()); // [1, 21, 30, 4]
    

31. toLocaleString() 메서드:

  • 설명: 배열의 요소를 각 언어의 로케일을 사용해 문자열로 반환합니다.
  • 사용 예시:
    1
    2
    3
    
    const array1 = [1, 'a', new Date('21 Dec 1997 14:12:00 UTC')];
    const localeString = array1.toLocaleString('en', { timeZone: 'UTC' });
    console.log(localeString); // "1,a,12/21/1997, 2:12:00 PM"
    

32. toString() 메서드:

  • 설명: 배열과 그 요소들을 문자열로 반환합니다.
  • 사용 예시:
    1
    2
    
    const array1 = [1, 2, 'a', '1a'];
    console.log(array1.toString()); // "1,2,a,1a"
    

33. values() 메서드:

  • 설명: 배열의 각 인덱스에 대한 값을 가지는 새로운 Array Iterator 객체를 반환합니다.
  • 사용 예시: ```javascript const array1 = [‘a’, ‘b’, ‘c’]; const iterator = array1.values();

for (const value of iterator) { console.log(value); // ‘a’, ‘b’, ‘c’ } ```

이렇게 오늘 나머지 17개 정리를 해봤습니다.

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

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