โ ๋ฐฐ์ด.filter(์ฝ๋ฐฑํจ์(๋ฐฐ์ด์ ๊ฐ, ๋ฐฐ์ด์ ์ธ๋ฑ์ค, ๋ฐฐ์ด), thisArg)
โ ๋ฐฐ์ด์ ์ฝ๋ฐฑํจ์์ return๊ฐ์ด true์ธ ๊ฒฝ์ฐ์๋ง ๊ทธ ์ธ์๋ค์ ๋ชจ์์ ๋ฐํํ๋ค.
โ ์ฝ๋ฐฑํจ์๋ฅผ ๋ฐ๋ก ๋ถ๋ฌ์ค์ง ์๊ณ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ์๋ ES6 ํ์ดํ ํจ์๋ ํจ์์ ์ธ์์ผ๋ก ๋ฃ๋๋ค.
โ ๋ฐฐ์ด.filter((๋ฐฐ์ด์ ๊ฐ, ๋ฐฐ์ด์ ์ธ๋ฑ์ค, ๋ฐฐ์ด)=>{...์คํ๋ฌธ return ...}, thisArg)
โ ์ฝ๋ฐฑํจ์๋ฅผ ์ธ์๋ก ๋ฐ๋ ๊ฒ์ ๊ณ ์ฐจํจ์๋ผ๊ณ ํ๋ค.
โ return๊ฐ์ด true์ธ ์ธ์๋ค์ ๋ชจ์ ์๋ก์ด ๋ฐฐ์ด์ ๋ฐํํ๊ธฐ ๋๋ฌธ์ ์๋ณธ๋ฐฐ์ด์ ๋ณ๊ฒฝ๋์ง ์๋๋ค.
filter๋ฉ์๋๋ ๋ฐฐ์ด์ ๋ชจ๋ ๊ฐ๋ค์ ์ํํ๋ค.
const arr = [1,2,3,4,5]; const filter = arr.filter((value, index, array) => { console.log(value, index, array); }) console.log(filter); // 1๋ฒ์คํ 1 0 [ 1, 2, 3, 4, 5 ] // 2๋ฒ์คํ 2 1 [ 1, 2, 3, 4, 5 ] // 3๋ฒ์คํ 3 2 [ 1, 2, 3, 4, 5 ] // 4๋ฒ์คํ 4 3 [ 1, 2, 3, 4, 5 ] // 5๋ฒ์คํ 5 4 [ 1, 2, 3, 4, 5 ]
๐ก ์ด์ ๊ฐ์ด ๋ชจ๋ ๊ฐ๋ค์ ์ํํ๋ค. ์ฝ๊ฒ ์๊ฐํด ๋ณด๋ฉด arr์ 0๋ฒ ์ธ๋ฑ์ค๋ถํฐ arr.length - 1์ ์ธ๋ฑ์ค๊น์ง for๋ฌธ์ด ๋๊ณ ์๋ค๊ณ ์๊ฐํ๋ฉด ์ฝ๋ค.
filter๋ฉ์๋๋ ๋ฐฐ์ด์ ๋ชจ๋ ๊ฐ๋ค์ ์ํํ๋ฉด์ return๊ฐ์ด true์ธ ๊ฐ๋ค์ ๋ชจ์์ ๋ฐํํ๋ค.
const arr = [1,2,3,4,5]; const filter = arr.filter((value) => { return value > 3; // value๊ฐ 3๋ณด๋ค ํฐ ๊ฐ๋ค์ ๋ชจ์์ ์๋ก์ด ๋ฐฐ์ด๋ก ๋ฐํํ๋ค. }) console.log(filter); // [4,5] ์ถ๋ ฅ
indexOf๋ฅผ ์ฌ์ฉํ์ฌ ์ค๋ณต์ ๊ฑฐ๋ฅผ ํ ์ ์๋ค.
const arr = [1,1,1,2,2,2,3,3,3]; /* arr.indexOf(value)์์ value๊ฐ์ด ๋ค์ด๊ฐ index์ ์ฒซ ์๋ฆฌ๊ฐ ๋ฐํ๋๋ค. ๊ทธ๋ฌ๋ฏ๋ก filter์ index๊ฐ indexOf(value)์ธ ๊ฐ๊ณผ ์ผ์นํ๋ฉด ๋ฐํํ๊ฒ ๋๋ฉด ์ค๋ณต๊ฐ์ ๊ฑฐ๊ฐ ์๋ฃ๋๋ค. */ const filter = arr.filter((value, index) => arr.indexOf(value) === index); console.log(filter); // [1,2,3] ์ถ๋ ฅ
includes๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ง๊ณ ์ํ๋ ๊ฐ๋ง ์ถ์ถํ ์ ์๋ค.
const arr = [1,3,5,6,9,10,12,15,18,20]; const compare = [3,6,9,12,15,18]; // 3์ ๋ฐฐ์๋ก ์ถ์ถํ ๋ฐฐ์ด์ ์์ฑ // includes๋ฉ์๋๋ ํด๋น ๋ฐฐ์ด์ ๊ทธ ๊ฐ์ด ์์ผ๋ฉด true๋ฅผ ๋ฐํํ๋ค. const filter = arr.filter((value) => compare.includes(value)); console.log(filter); // [3,6,9,12,15,18] ์ถ๋ ฅ
๐ก ๋ฌธ์์ด์ split()๋ฉ์๋๋ก ๋๋๊ณ ๊ทธ ๋ฌธ์์ด์์ ํน์๋ฌธ์ ์ถ์ถ๋ ๊ฐ๋ฅํ๋ค.
filter๋ฉ์๋ ๋ค์ thisArg๋ผ๋ ๊ฒ์ด ๋ฌด์์ธ์ง ์์๋ณด๋ฉด ์ ์๋ callback์ ์คํํ ๋ this๋ก ์ฌ์ฉํ๋ ๊ฐ์ด๋ผ๋ ๋ป์ด๋ค. ํ๋ฒ ์์๋ณด์.
const filter = [1].filter(function () { console.log(this); }) console.log(filter); // ์ถ๋ ฅ๊ฐ window๊ฐ์ฒด ์ถ๋ ฅ
window๊ฐ์ฒด๊ฐ ์ถ๋ ฅ๋๋ค. (์น ํ์ด์ง์์ ์คํ์) node.js๋ก ์คํ์ํค๋ฉด global๊ฐ์ฒด๊ฐ ์ถ๋ ฅ๋๋ค.
// ์์ฑ์ ํจ์ ์์ฑ function compare() { this.num = 3; } // ์ธ์คํด์ค ์์ฑ const newCompare = new compare(); const arr = [1,3,5,6,9,10,12,15,18,20]; const filter = arr.filter(function(value) { console.log(this); // ์ถ๋ ฅ๊ฐ compare ์ถ๋ ฅ return this.num < value; }, newCompare) console.log(filter); // [5,6,9,10,12,15,18,20] ์ถ๋ ฅ
์ฌ๊ธฐ์ ํ์ธํด๋ณด๋ฉด this๊ฐ compare๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ๋์ด compare์ ์๋ this.num์ ํธ์ถ ํ ์ ์๊ฒ ๋๊ฒ์ด๋ค.
function compare() { this.num = 3; } const newCompare = new compare(); const arr = [1,3,5,6,9,10,12,15,18,20]; const filter = arr.filter((value) => { console.log(this); // ์ถ๋ ฅ๊ฐ window๊ฐ์ฒด ์ถ๋ ฅ return this.num < value; // this.num === undefined }, newCompare) console.log(filter); // [] ์ถ๋ ฅ
์ด์ ๊ฐ์ด ES6๋ฌธ๋ฒ์ ์ฌ์ฉํ๋ฉด window๊ฐ์ฒด๊ฐ ์ถ๋ ฅ๋๋ค.
โ ES6 ํ์ดํ ๋ฌธ๋ฒ์ this๋ฅผ ๋ฐ์ธ๋ฉ ํ์ง ์๊ธฐ ๋๋ฌธ์ ์๊ธฐ๋ ๋ฌธ์ ์ ์ด๋ค.
โ ์ธ์คํดํธ์ this๋ ์์ฑ์ํจ์๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
โ thisArg์ ๊ฐ๋ ์ ์ดํด ํ ๋ ค๋ฉด this๋ฅผ ์ดํด๊ฐ ํ์ํ๋ค.
โ ๊ทธ๋ฌ๋ฏ๋ก thisArg๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด array.filter(function() {}, thisArg)๋ก ์ฌ์ฉํด์ผ ํ๋ค.
โ function() {} ์๋ฆฌ์๋ ํจ์๋ฅผ ์ ์ํด์ ์ฌ์ฉํด๋ ๋์ง๋ง ES6ํ์์ผ๋ก ์ฌ์ฉํ๋ฉด ์๋๋ค.
โ ๋์ค์ this์ ๋ํด์ ์์ธํ ์ ๋ฐ์ดํธ ํ ์์ ์ด๋ค.
์ถ์
MDN = Array.prototype.filter()