02. filter

์–‘ํฌ์ค€ยท2021๋…„ 10์›” 3์ผ
0

JS Array Method

๋ชฉ๋ก ๋ณด๊ธฐ
2/7
post-thumbnail

๐Ÿ“Œ 2-1 filter๋ž€?

โœ” ๋ฐฐ์—ด.filter(์ฝœ๋ฐฑํ•จ์ˆ˜(๋ฐฐ์—ด์˜ ๊ฐ’, ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค, ๋ฐฐ์—ด), thisArg)
โœ” ๋ฐฐ์—ด์˜ ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ return๊ฐ’์ด true์ธ ๊ฒฝ์šฐ์—๋งŒ ๊ทธ ์ธ์ž๋“ค์„ ๋ชจ์•„์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
โœ” ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ๋”ฐ๋กœ ๋ถˆ๋Ÿฌ์˜ค์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ES6 ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋‚˜ ํ•จ์ˆ˜์„ ์–ธ์‹์œผ๋กœ ๋„ฃ๋Š”๋‹ค.
โœ” ๋ฐฐ์—ด.filter((๋ฐฐ์—ด์˜ ๊ฐ’, ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค, ๋ฐฐ์—ด)=>{...์‹คํ–‰๋ฌธ return ...}, thisArg)
โœ” ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š” ๊ฒƒ์„ ๊ณ ์ฐจํ•จ์ˆ˜๋ผ๊ณ  ํ•œ๋‹ค.
โœ” return๊ฐ’์ด true์ธ ์ธ์ž๋“ค์„ ๋ชจ์•„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์›๋ณธ๋ฐฐ์—ด์€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š”๋‹ค.

๐Ÿ“Œ 2-2 filter ์ž‘๋™์˜ˆ์ œ

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๋ฌธ์ด ๋Œ๊ณ  ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ์‰ฝ๋‹ค.

๐Ÿ“Œ 2-3 filter ์‚ฌ์šฉ์˜ˆ์ œ

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] ์ถœ๋ ฅ

๐Ÿ“Œ 2-4 filter ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‚ฌ์šฉ์˜ˆ์ œ 1.

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] ์ถœ๋ ฅ

๐Ÿ“Œ 2-5 filter ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‚ฌ์šฉ์˜ˆ์ œ 2.

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()๋ฉ”์„œ๋“œ๋กœ ๋‚˜๋ˆ„๊ณ  ๊ทธ ๋ฌธ์ž์—ด์•ˆ์— ํŠน์ˆ˜๋ฌธ์ž ์ถ”์ถœ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

๐Ÿ“Œ 2-6 thisArg

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๊ฐ์ฒด๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

๐Ÿ“Œ 2-7 thisArg ์ •๋ฆฌ

โœ” ES6 ํ™”์‚ดํ‘œ ๋ฌธ๋ฒ•์€ this๋ฅผ ๋ฐ”์ธ๋”ฉ ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ƒ๊ธฐ๋Š” ๋ฌธ์ œ์ ์ด๋‹ค.
โœ” ์ธ์Šคํ„ดํŠธ์˜ this๋Š” ์ƒ์„ฑ์žํ•จ์ˆ˜๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
โœ” thisArg์˜ ๊ฐœ๋…์„ ์ดํ•ด ํ• ๋ ค๋ฉด this๋ฅผ ์ดํ•ด๊ฐ€ ํ•„์š”ํ•˜๋‹ค.
โœ” ๊ทธ๋Ÿฌ๋ฏ€๋กœ thisArg๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด array.filter(function() {}, thisArg)๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
โœ” function() {} ์ž๋ฆฌ์—๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ด์„œ ์‚ฌ์šฉํ•ด๋„ ๋˜์ง€๋งŒ ES6ํ˜•์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋œ๋‹ค.
โœ” ๋‚˜์ค‘์— this์— ๋Œ€ํ•ด์„œ ์ž์„ธํžˆ ์—…๋ฐ์ดํŠธ ํ•  ์˜ˆ์ •์ด๋‹ค.

์ถœ์ €
MDN = Array.prototype.filter()

profile
JS ์ฝ”๋ฆฐ์ด

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