자바스크립트에 빌트인되어있는 배열 메서드를 반복 메서드(iteration methods) 혹은 반복자
라고 부른다. 반복자는 요소를 조작하고 값을 반환하기 위해 배열에서 호출되는 메서드다.
undefined
이다.const fruits = ['mango', 'papaya', 'pineapple', 'apple'];
// 기본 작성법
fruits.forEach(function(fruitsItem) { //fruits가 식별자가 된다.
console.log(fruitsItem);
});
//화살표함수 작성법 **주로 씀**
fruits.forEach(fruitsItem =>
console.log(fruitsItem));
//함수선언식 작성법
function printFruit(element) {
console.log(element);
}
fruits.forEach(printFruit);
//
mango
papaya
pineapple
apple 동일값 반환
const animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog'];
const secretMessage = animals.map(letter => {
return letter[0];
});
console.log(secretMessage.join(''));
//HelloWorld 반환
const bigNumbers = [100, 200, 300, 400, 500];
const smallNumbers = bigNumbers.map(divide => {
return divide/100;
})
console.log(smallNumbers);
//[ 1, 2, 3, 4, 5 ] 반환
const randomNumbers = [375, 200, 3.14, 7, 13, 852];
const smallNumbers = randomNumbers.filter(number => {
return number < 250;
})
console.log(smallNumbers);
//[ 200, 3.14, 7, 13 ]반환
const favoriteWords = ['nostalgia', 'hyperbole', 'fervent', 'esoteric', 'serene'];
const longFavoriteWords = favoriteWords.filter(word => {
return word.length > 7;
})
console.log(longFavoriteWords);
// [ 'nostalgia', 'hyperbole', 'esoteric' ]반환
const animals = ['hippo', 'tiger', 'lion', 'seal', 'cheetah', 'monkey', 'salamander', 'elephant'];
const foundAnimal = animals.findIndex(animal => {
return animal === 'elephant';
});
console.log(foundAnimal); //7반환
const startsWithS = animals.findIndex(withS => {
return withS[0] === 's';
})
console.log(startsWithS); //3반환
const newNumbers = [1, 3, 5, 7];
const newSum = newNumbers.reduce((accumulator, currentValue) => {
console.log('The value of accumulator: ', accumulator);
console.log('The value of currentValue: ', currentValue);
return accumulator + currentValue;
}, 10); // <-- .reduce() 메서드는 두번째 매개변수로 초기값을 설정할 수 있다.(옵션값)
console.log(newSum);
//
The value of accumulator: 10
The value of currentValue: 1
The value of accumulator: 11
The value of currentValue: 3
The value of accumulator: 14
The value of currentValue: 5
The value of accumulator: 19
The value of currentValue: 7
26 반환
** 참고 (동일한 반복자 작성법)
const newSum = newNumbers.reduce(function(accumulator, currentValue){
// Existing code...
}, 10)
const newSum = newNumbers.reduce((accumulator, currentValue) => {
// Existing code...
}, 10)
이외에도 다양게 빌트인되어 있는 배열메서드가 있다.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Iteration_methods 참조
-간단한 정의
-메서드를 사용하는 올바른 문법
-메서드가 요구하는 매개변수 리스트
-함수의 반환값
-좀 더 긴 설명
-메서드 사용 예제
-추가적 정보 등을 담고 있다.
const words = ['unique', 'uncanny', 'pique', 'oxymoron', 'guise'];
console.log(words.some((word) => {
return word.length < 6;
})); // true반환 (배열의 요소 중 하나라도 조건을 만족할 경우 true)
const interestingWords = words.filter((word) => {
return word.length > 5;
});
console.log(interestingWords); //['unique', 'uncanny', 'oxymoron']반환
console.log(interestingWords.every((word) => {
return word.length > 5;
} )); // // true반환 (배열의 모든 요소가 조건을 만족할 경우 true)