FE/JavaScript

[JS] ๋ฐฐ์—ด์˜ ๋ฉ”์†Œ๋“œ - find , filter ,map

mandelina 2022. 5. 11. 01:51

๐Ÿš€ ๋ฐฐ์—ด์ƒ์„ฑ 

 

[๋ฐฉ๋ฒ•1]

๊ฐ์ฒด์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ด๊ณ  ๊ฐ„ํŽธํ•œ ๋ฐฉ์‹์€ ๋ฐฐ์—ด ๋ฆฌํ„ฐ๋Ÿด์„ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

//๋ฐฉ๋ฒ•1
const arr1 = [1,2,3];
console.log (arr1.length); //3 

//๋ฐฉ๋ฒ•2
const arr2 = []; 
console.log(arr2.length) //0

//๋ฐฉ๋ฒ•3 
const arr3 = [1, , 3]; //ํฌ์†Œ๋ฐฐ์—ด 
console.log(arr3.length) //3

 

- ๋ฐฐ์—ด ๋ฆฌํ„ฐ๋Ÿด์€ 0๊ฐœ์ด์ƒ ์š”์†Œ๋ฅผ ์‰ผํ‘œ๋กœ ๊ตฌ๋ณ„ํ•˜๋ฉฐ , [] ๋กœ ๋ฌถ๋Š”๋‹ค.

- ๋ฐฉ๋ฒ•3์—์„œ์˜ arr3์„ ํฌ์†Œ๋ฐฐ์—ด (length๊ฐ€ ์‹ค์ œ์š”์†Œ๊ฐœ์ˆ˜๋ณด๋‹ค ํฐ ๋ฐฐ์—ด)์ด๋ผ ํ•œ๋‹ค.

 

(ํ•˜์ง€๋งŒ ํฌ์†Œ๋ฐฐ์—ด์€ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๊ฒƒ์ด ์ข‹๋‹ค. ๋ฐฐ์—ด๊ณผ ๊ฐœ๋…์— ๋งž์ง€ ์•Š์œผ๋ฉฐ ์„ฑ๋Šฅ์—๋„ ์•ˆ์ข‹์€ ์˜ํ–ฅ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ)

 

 

[๋ฐฉ๋ฒ•2]

Array ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

const arr1 = new Array(10); // 10๊ฐœ์˜ ๋นˆ๋ฐฐ์—ด ์ƒ์„ฑ

const arr2 = new Array(1,2,3); // [1,2,3] ์ƒ์„ฑ

 

 


 

๐Ÿš€ ๋ฐฐ์—ด๋ฉ”์†Œ๋“œ - find 

 

- ํŠน์ • ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š” ๊ฐ’์˜ ์ฒซ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

- filter์™€ ์œ ์‚ฌํ•˜๋‚˜ , ๋‹จ ํ•˜๋‚˜์˜ ์š”์†Œ๋งŒ์„ ์ฐพ๋Š”๋‹ค๋Š” ์ ์ด ๋‹ค๋ฅด๋‹ค.

- ์–ด๋–ค ์š”์†Œ๋„ ํŠน์ • ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜์ง€ ๋ชปํ•˜๋ฉด undefined ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

- ์›๋ณธ์ˆ˜์ • X

 

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

arr.find(i => i > 5);   // 5๋ณด๋‹ค ํฐ ์ˆซ์ž์ค‘ ๊ฐ€์žฅ ์ฒซ๋ฒˆ์งธ ์š”์†Œ์ธ 6๋ฐ˜ํ™˜

 

 

๐Ÿš€ ๋ฐฐ์—ด๋ฉ”์†Œ๋“œ - filter

 

- ํŠน์ • ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š” ๊ฐ’์„ ์ฐพ๊ณ  ๊ทธ ๊ฐ’๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค .

- ์ฆ‰, ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์ด true์ธ ๋ชจ๋“  ์š”์†Œ๋ฅผ ๋ชจ์•„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด ์ถœ๋ ฅํ•œ๋‹ค.

- ์›๋ณธ์ˆ˜์ • X

 

  class Student {
        constructor(name, age, enrolled, score) {
          this.name = name;
          this.age = age;
          this.enrolled = enrolled;
          this.score = score;
        }
      }
      const students = [
        new Student("A", 29, true, 45),
        new Student("B", 28, false, 80),
        new Student("C", 30, true, 90),
        new Student("D", 40, false, 66),
        new Student("E", 18, true, 88),
      ];
      
     const result = students.filter((student) => student.enrolled);
     console.log(result);

student.enrolled ๊ฐ€ true์ธ ์š”์†Œ๋ฅผ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด ์ถœ๋ ฅํ•œ๋‹ค.

 

 

[๊ฒฐ๊ณผ]


 

 

๐Ÿš€ ๋ฐฐ์—ด๋ฉ”์†Œ๋“œ - map

 

- ์ด๋ฆ„๊ณผ ๊ฐ™์ด ๋ฐฐ์—ด์˜ ์š”์†Œ๋ฅผ ๋Œ๋ฉฐ ์š”์†Œ๋“ค์„ 1๋Œ€1 mapping์„ ํ•ด์ค€๋‹ค.

- ๊ธฐ์กด์˜ ๊ฐ’์„ ์žฌ์ •์˜ํ•  ์ˆ˜๋„ ์žˆ๊ณ , ์ƒˆ๋กœ์šด ํ˜•ํƒœ์˜ ๊ฐ’์„ ์ •์˜ํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

- ์›๋ณธ์ˆ˜์ • X

 

      class Student {
        constructor(name, age, enrolled, score) {
          this.name = name;
          this.age = age;
          this.enrolled = enrolled;
          this.score = score;
        }
      }
      const students = [
        new Student("A", 29, true, 45),
        new Student("B", 28, false, 80),
        new Student("C", 30, true, 90),
        new Student("D", 40, false, 66),
        new Student("E", 18, true, 88),
      ];
      
   //  make an array containing only the students' scores
   // result should be: [45, 80, 90, 66, 88]
   
     const result = students.map((students) => students.score);
     console.log(result);

students.score๋กœ ๋งคํ•‘ํ•˜์—ฌ result๊ฐ’์— ์ €์žฅํ•œ๋‹ค.

 

[๊ฒฐ๊ณผ]

 


 

 

๋˜ํ•œ  Array.prototype.filter() ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•จ์œผ๋กœ์จ Method Chaining ์„ ํ†ตํ•ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

* ๋ฉ”์†Œ๋“œ ์ฒด์ด๋‹ ?  ์—ฌ๋Ÿฌ ๋ฉ”์„œ๋“œ๋ฅผ ์ด์–ด์„œ ํ˜ธ์ถœํ•˜๋Š” ๋ฌธ๋ฒ•

 

const arr = [{
    'name' : 'title1',
    'contents' : 'contents1',
    'dataNum' : 1,
    'data' : [1, 2, 3]
}, {
    'name' : 'title2',
    'contents' : 'contents2',
    'dataNum' : 2,
    'data' : [1, 2, 3]
}, {
    'name' : 'title3',
    'contents' : 'contents3',
    'dataNum' : 3,
    'data' : [1, 2, 100]
}, {
    'name' : 'title4',
    'contents' : 'contents4',
    'dataNum' : 4,
    'data' : [1, 2, 3]
}, {
    'name' : 'title5',
    'contents' : 'contents5',
    'dataNum' : 5,
    'data' : [1, 2, 100]
}];

arr.map((i) => {
    if (i.data.includes(100)) {
        return i.name
    }
    return 
}).filter(i => i);
//expected output: ['title3', 'title5']

 

map์„ํ†ตํ•ด data์— 100๋‹ถ์ด ๋“ค์–ด์žˆ๋Š” name์„ ๋ฐฐ์—ด์— ๋งคํ•‘ํ•˜๊ณ  , ๊ทธ๊ฐ’๋“ค์ค‘ undefined ๋œ ๊ฐ’์„ ์ œ์™ธํ•œ ๊ฐ’๋“ค๋งŒ์„ filter๋กœ ์ถœ๋ ฅํ–ˆ๋‹ค.

 

[๊ฒฐ๊ณผ]


- map์„ ์‚ฌ์šฉํ–ˆ์„๋•Œ ๊ฐ’



 

- map์„ ์‚ฌ์šฉํ•œ ํ›„ filter์„ ์‚ฌ์šฉํ•˜์˜€์„๋•Œ ๊ฐ’

 

* ์ฐธ๊ณ ๋กœ ์›๋ณธ๋ฐฐ์—ด ์ž์ฒด๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.  

 

push()  pop() shift() unshift()