JavaScript 배열 forEach()
실시예 1
과일의 각 요소에 대한 함수를 호출합니다.
const fruits = ["apple", "orange", "cherry"];
fruits.forEach(myFunction);
정의 및 사용
이 forEach()
메서드는 배열의 각 요소에 대한 함수를 호출합니다.
forEach()
메서드는 빈 요소에 대해 실행되지 않습니다 .
통사론
array.forEach(function(currentValue, index, arr), thisValue)
매개변수
function() | Required. A function to run for each array element. |
currentValue | Required. The value of the current element. |
index | Optional. The index of the current element. |
arr | Optional. The array of the current element. |
thisValue | Optional. Default undefined .A value passed to the function as its this value. |
반환 값
찾으시는 주소가 없습니다 |
브라우저 지원
forEach()
ECMAScript5(ES5) 기능입니다.
모든 브라우저에서 완전히 지원되는 ES5(JavaScript 2009):
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |
더 많은 예
합계를 계산합니다.
let sum = 0;
const numbers = [65, 44, 12, 4];
numbers.forEach(myFunction);
function myFunction(item) {
sum += item;
}
각 요소를 곱합니다.
const numbers = [65, 44, 12, 4];
numbers.forEach(myFunction)
function
myFunction(item, index, arr) {
arr[index] = item * 10;
}