JS) filter()

Ceciliaยท2022๋…„ 12์›” 20์ผ
0

JavaScript

๋ชฉ๋ก ๋ณด๊ธฐ
24/36
post-thumbnail

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




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

filter()๋Š” ๋ฐฐ์—ด ๋‚ด ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ํ•œ ๋ฒˆ ์ œ๊ณต๋œ callback ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด, callback์ด true๋กœ ๊ฐ•์ œํ•˜๋Š” ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ชจ๋“  ๊ฐ’์ด ์žˆ๋Š” ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ๋‹ค.


function isBigEnough(value) {
  return value >= 10;
}

var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
// filtered ๋Š” [12, 130, 44]
profile
'์ด๊ฒŒ ์ตœ์„ ์ผ๊นŒ?'๋ผ๋Š” ๊ณ ์ฐฐ์„ ํ†ตํ•ด ๋์—†์ด ์„ฑ์žฅํ•˜๊ณ , ๊ทธ ๊ณผ์ •์„ ์ฆ๊ธฐ๋Š” ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž์˜ ์ž…์žฅ์„ ์ƒ๊ฐํ•˜๋ฉฐ ์ตœ์„ ์˜ ํŽธ์˜์„ฑ์„ ์ฐพ๊ธฐ ์œ„ํ•ด ๋…ธ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€