filter, map, every

sang hyeok Lee·2022년 3월 21일
0

filter

filter는 배열데이터에서 사용가능한 함수이다. 특정조건에 맞는 모든 원소를 새로운 배열로 만드러 준다.

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"]

words라는 배열에 filter을 통해서 각 요소들을 순회하면서 글자의 길이가 6을 넘는 요소들을 골라서 새로운 배열을 만들어 주고 result에 그 결과값을 담아주고 출력한 결과이다.


map

map은 배령에서 사용가능하며 각 요소들을 순회허면 주어진 함수를 호출한 결과를 새로운 배열로 반환한다.

const array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map(x => x * 2);

console.log(map1);

array1이라는 배열에서 map을 통해서 각 요소를 순회하면서 x * 2라는 함수를 실행고 그 결과값들을 모아 새로운 배열로 반환을 한다.


every

every는 주어진 배열의 모든 요소가 판별함수를 통과하는지 테스트를 한다. 결과값으로 boolearn값을 반환한다.

const isBelowThreshold = (currentValue) => currentValue < 40;

const array1 = [1, 30, 39, 29, 10, 13];

console.log(array1.every(isBelowThreshold));

array1 배열이 every를 통하여 순회하면서 판별함수를 거쳐 조건에 부합되는지 확인한다. array1 배열은 모든 원소가 부합함으로 true값을 반환한다.

profile
개발자 되기

0개의 댓글

관련 채용 정보