---
# System prepended metadata

title: 【Filter】、【Find】過濾 練習
tags: [JS - 練習]

---

---
tags: JS - 練習
---
# 【Filter】、【Find】過濾 練習

:::warning

### filter

> filter() 方法創建一個新的數組，新數組中的元素是通過檢查指定數組中符合條件的所有元素。

例如：
```
    const result = studentsData.filter(item => item.age >= 18);
    // 執行結果：result 為 [
        { name: '王大偉', age: '18'},
        { name: '陳小名', age: '24'}...
    ]
    
    const result = studentsData.filter(item => item.age >= 70);
    // 執行結果：result 為 []
```

### find

> find() 方法返回數組中滿足提供的測試函數的第一個元素的值。否則返回 undefined。

例如：

```
    const result = studentsData.find(item => item.age >= 18);
    // 執行結果：result 為 { name: '王大偉', age: '18'}
    
    const result = studentsData.find(item => item.age >= 70);
    // 執行結果：result 為 undefined
```
:::



> 請用下列資料，data為學校資料:

```
  const data = [
      {
        class: '甲',
        studentsNum: '10',
        studentsData: [
            { id: 1, name: '王大偉', age: 18, interest: ['聽音樂', '唱歌', '看書'] },
            { id: 2, name: '陳小名', age: 24, interest: ['打球', '聽音樂'] },
            { id: 3, name: '許世斌', age: 23, interest: ['唱歌', '聽音樂'] },
            { id: 4, name: '吳添財', age: 25, interest: ['打球', '聽音樂'] },
            { id: 5, name: '陳漢霖', age: 66, interest: ['喝酒', '唱歌', '看書'] },
            { id: 6, name: '楊采媗', age: 17, interest: ['打球', '唱歌'] },
            { id: 7, name: '曾無傭', age: 34, interest: ['打球'] },
            { id: 8, name: '楊漢鎮', age: 38, interest: ['看書', '唱歌'] },
            { id: 9, name: '楊是崁', age: 35, interest: ['看書'] },
            { id: 10, name: '楊安浪', age: 50, interest: ['聽音樂', '唱歌', '看書'] }
        ]
      },
      {
        class: '乙',
        studentsNum: '5',
        studentsData: [
            { id: 1, name: '蔡申明', age: 50, interest: ['打球', '唱歌', '看書'] },
            { id: 2, name: '蔡文旯', age: 51, interest: ['唱歌', '看書'] },
            { id: 3, name: '陳炘採', age: 58, interest: ['打球'] },
            { id: 4, name: '昂天次', age: 56, interest: ['聽音樂', '唱歌', '看書'] },
            { id: 5, name: '王銘感', age: 58, interest: ['聽音樂'] }
        ]
      },
      {
        class: '丙',
        studentsNum: '6',
        studentsData: [
            { id: 1, name: '林赤子', age: 18, interest: ['唱歌'] },
            { id: 2, name: '林小優', age: 24, interest: ['聽音樂', '唱歌', '看書'] },
            { id: 3, name: '陳問鼎', age: 23, interest: ['打球', '唱歌', '看書'] },
            { id: 4, name: '陳字字', age: 25, interest: ['打球', '唱歌', '聽音樂'] },
            { id: 5, name: '陳余文', age: 66, interest: ['聽音樂', '唱歌', '看書'] },
            { id: 6, name: '楊蔡文', age: 17, interest: ['打球', '聽音樂'] }
        ]
      }
      
  ]
```

### Filter 題目:

:::info
請將甲班的姓"楊"的同學篩選出來
:::

:::info
請將丙班的同學年齡大於30歲的同學篩選出來
:::

:::info
請將所有班級姓"陳"且年齡大於20歲的同學篩選出來，並放在同一個陣列
:::

:::info
請將甲班喜歡"聽音樂的人篩選出來"
:::

### Find 題目:

:::info
請將甲班的姓"許"的第一個同學篩選出來
:::

:::info
請將乙班的同學興趣為打球的學生篩出來
:::

:::info
判斷該學校中是否有戊班
:::


### 補充練習 題目 (不限制方法):

:::info
請整理出甲班大於20歲，且喜歡"打球"的同學篩選出來
:::

:::info
請整理出學校學生興趣是"看書"且興趣是三個的同學
:::