[JS] λ°°μ΄μ λ©μλ - find , filter ,map
π λ°°μ΄μμ±
[λ°©λ²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() |



