JS메소드(2)

BOONG GI JUNG·2023년 12월 27일

Language

목록 보기
9/13

map(callback(currentValue, index, array), thisArg)
배열의 각 요소에 대해 주어진 함수를 호출하고, 그 결과로 새로운 배열을 생성합니다.

const numbers = [1, 2, 3, 4, 5];
const squaredNumbers = numbers.map(function (number) {
  return number * number;
});
// 결과: [1, 4, 9, 16, 25]

reduce(callback(accumulator, currentValue, index, array), initialValue)
배열의 각 요소에 대해 주어진 함수를 실행하고, 누적값(accumulator)을 반환합니다.

const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce(function (accumulator, number) {
  return accumulator + number;
}, 0);
// 결과: 15

forEach(callback(currentValue, index, array))
배열의 각 요소에 대해 주어진 함수를 실행합니다. 반환값이 없습니다.

const numbers = [1, 2, 3, 4, 5];
numbers.forEach(function (number) {
  console.log(number);
});
// 출력: 1, 2, 3, 4, 5

find(callback(currentValue, index, array), thisArg)
주어진 조건을 만족하는 첫 번째 요소를 반환합니다.

const numbers = [1, 2, 3, 4, 5];
const foundNumber = numbers.find(function (number) {
  return number > 2;
});
// 결과: 3

some(callback(currentValue, index, array), thisArg)
배열 중 하나 이상의 요소가 주어진 조건을 만족하면 true를 반환합니다.

const numbers = [1, 2, 3, 4, 5];
const hasEvenNumber = numbers.some(function (number) {
  return number % 2 === 0;
});
// 결과: true

every(callback(currentValue, index, array), thisArg)
배열의 모든 요소가 주어진 조건을 만족하면 true를 반환합니다.

const numbers = [2, 4, 6, 8, 10];
const allEvenNumbers = numbers.every(function (number) {
  return number % 2 === 0;
});
// 결과: true
profile
새로운 기술을 즐기는 라이프 하루에 한번 포스팅하기!

0개의 댓글