Front/JS & jQuery

[javascript] array.forEach() / array.filter()

오선지♬ 2022. 4. 21. 20:16
728x90
반응형

forEach() 

forEach() 메서드는 주어진 함수를 배열 요소 각각에 대해 실행한다.

ex)

const array1 = ['a', 'b', 'c'];

array1.forEach(element => console.log(element));

// expected output: "a"
// expected output: "b"
// expected output: "c"

 

참고 : https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

 

 

filter()

filter() 메서드는 주어진 함수의 테스트를 통과하는 요소를 모아 새로운 배열로 반환한다.

ex)

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

 

참고 : https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

728x90
반응형

'Front > JS & jQuery' 카테고리의 다른 글

[javascript] 소수점 계산 오류  (0) 2022.04.23
[javascript] 배열의 최대값, 최소값 구하기  (0) 2022.04.22
[javascript] async & await  (0) 2022.04.20
[jQuery] .html() .text() .val() 의 차이  (0) 2022.04.19
[javascript] Promise  (0) 2022.04.18