filter 方法 - daniel-qa/Vue GitHub Wiki

filter 方法

以下範例展示了如何使用 .filter 方法來過濾通知歷史記錄。根據用戶輸入的關鍵字、目標和方法進行篩選。

範例說明

// 過濾邏輯
filteredNoticeHistory.value = noticeHistory.value.filter((item) => {
    const matchesKeyword = !selectForm.value.keyword || item.content.includes(selectForm.value.keyword);
    const matchesTarget = !selectForm.value.target.length || selectForm.value.target.includes(item.target);
    const matchesMethod = !selectForm.value.method.length || selectForm.value.method.includes(item.selType);

    return matchesKeyword && matchesTarget && matchesMethod;
});
  • 主要關鍵是,只要返回 true,該筆資料就會保留; 配合 item.content.includes(),可以設定篩選條件。

  • 這個範例中,return 會把每個條件都做 and,所以必須全部命中,才會保留。