View
map :
새로운 배열을 반환한다.
새로운 배열은 원본 배열 객체에 연산을 거친 결과들을 객체로 가진다
각 객체를 순회하면서 해당연산 결과를 새로운 배열 객체로 반환한다
forEach와 다름
forEach는 배열을 순환하면서 어떤 행위를 side effect 로 수행하는 것 뿐, 하나씩하나씩..
어떤 일을 하는데.. 배열을 반환하는 것은 아니다!
map은 콜백함수를 통해서 새로운 값들을 배열의 객체로 반환한다, 끝나고 그 배열을 return 하는 것이다 한번에
filter:
새로운 배열을 반환한다
새로운 배열 객체는 filter에서 특정 테스트 조건을 통과한 것들만으로 구성된다.
filter()메서드는 주어진 함수의 테스트를 통과하는 모든 요소를 모아 새로운 배열로 반환한다.
즉 return 부분에 조건만 작성해도 그 조건을 통과하는 요소를 새로운 배열의 객체로 반환시킨다.
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]
word.length>6만 return해줘도, result는 ["exuberant", "destruction", "present"]가 된다.
참고)
1.
const result = words.filter(word => word.length > 6);는
const result = words. filter(function(word) {
return word.length>6;
});
과 같다.
2.
words.filter(콜백함수)
3. words.filter(function(word, i, words) {})
reduce:
배열이 반환되지 않는다.
reduce는 배열 객체 모두에 대해 reducer함수를 실행하여 하나의 값으로 반환한다.
사용 예: 배열 객체의 총 합 구하기, 배열 객체 중 가장 큰 수 구하기 등..
reply