프로그래밍/JavaScript
Functions - arrow, map, filter
L.Joey
2023. 7. 12. 20:44
화살표 함수 Arrow function
- 기본형태
let functionName = (argument1, arg2, ...argN) => expression;
let double = (n) => n * 2;
let double = function(n) {
return n*2;
}
- forEach(), map(), filter() 속에서도 가능
array.forEach(element => {
//code
});
- array.filter() 는 주어진 함수의 테스트를 통과하는 모든 요소를 모아 새로운 배열로 반환
const result = studentsInfo.filter(({ score }) => score >= 90);
//https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
- array.map() 는 배열 내의 모든 요소 각각에 대하여 주어진 함수를 호출한 결과를 모아 새로운 배열을 반환
names = result.map(({ name }) => name);
//https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/map
const array1 = [1, 4, 9, 16];
const map1 = array1.map(x => x * 2);