forEach , filter ,
forEach
forEach() ๋ array์์ ๊ฐ item์ ๋ํด function์ ์คํํ ์ ์๊ฒ ํด์ค๋ค.
array.forEach(function) ์์ผ๋ก ํ์ฉ๋๋ฉฐ,
์์ ๊ทธ๋ฆผ์ฒ๋ผ function์ ๋ฐ๋ก ์์ฑ ํ () ์์ ๋ฃ์ด๋ ๋๊ณ ,
() ์์์ ํ์
์ ์ ์ํด๋ ๋๋ค.
function Hello(item){ console.log("hello", item)}
array.forEach(Hello)
ํด์: function Hello๋ console์ hello + item ์ด๋ผ๊ณ ๋ณด์ฌ์ค ๊ฒ์ด๋ค.
array์ ๊ฐ item๋ค์ function Hello๋ฅผ ์คํ์์ผ๋ผ.
=
array.forEach((item) => console.log("hello", item))
ํด์: array์ ๊ฐ item๋ค์ console์ hello๋ฅผ +ํด์ ๋ณด์ฌ์ค ๊ฒ์ด๋ค.
filter
.fiter() ๋ array์์ ์์๋ค ์ค์ true ์ธ ๊ฒ๋ค๋ง ํํํ ์ ์๊ฒ ํด์ค๋ค. (false๋ฅผ ๋นผ์ค๋ค๊ณ ์๊ฐํด๋ ์ข์ ๊ฒ ๊ฐ๋ค.)
*array ์์ ์์๋ฅผ ์ญ์ ํ ๋ ๋ง์ด ์ฐ์ธ๋ค.
์๋๋ฐฉ์์ array.fiter(function) ์์, array ์์ ๊ฐ ์์๋ค์ foreach์ฒ๋ผ ํ๋ฒ์ฉ function์ ๋์ ํ์ฌ true๋ ์ด๋ฆฌ๊ณ , false๋ ์ฃฝ์ด๋ ๊ณผ์ ์ ๋ฐ๋ณตํ์ฌ ๊ฐ์ ์ป์ด๋ธ๋ค.
์ด๋, ์์ ๊ทธ๋ฆผ ์ฒ๋ผ, ํด๋น array ์์ฒด๊ฐ ๋ฐ๋๋๊ฒ์ ์๋๋ฏ๋ก, ์ฃผ์ํ๊ฑฐ๋ ์์ ๊ฐ์ด ์์
ํ ํ์๊ฐ ์๋ค.
(arr์ filter์ ํ์ง๋ง ์ฌ์ ํ arr์ ๊ทธ๋๋ก์ด๋ค. ๊ทธ๋์ arr.filter์ ๊ฐ์ธ newArr์ ์๋ก ๋ง๋ค์ด ํ์ ํ์ฉํ๋ ค ํ๋ ๋ชจ์ต์ด๋ค.)