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()