YYNotes

Code and Practice

#公开示例#JavaScript

Writing a Small Note Search

Match keywords with a small JavaScript function and connect input, processing, and output.

0 highlights0 highlights

Learning Objectives

  • Filter a list of notes with filter.
  • Understand why trimming and lowercasing help comparisons.

Key Concepts

Filtering keeps elements that satisfy a condition. Normalization puts inputs into a consistent form so unnecessary formatting differences do not affect comparison.

Public example: the following note list is demonstration data.

Main Content

A Complete Small Example

const notes = [
  { title: "Questions from Reading", topic: "Reading" },
  { title: "JavaScript Basics", topic: "Code" },
  { title: "The Mean", topic: "Concepts" },
];

function searchNotes(notes, query) {
  const keyword = query.trim().toLowerCase();
  return notes.filter((note) =>
    `${note.title} ${note.topic}`.toLowerCase().includes(keyword)
  );
}

console.log(searchNotes(notes, " javascript "));
// [{ title: "JavaScript Basics", topic: "Code" }]

Understand Each Step

  1. trim() removes spaces at the beginning and end of the query.
  2. toLowerCase() makes matching case-insensitive for English text.
  3. Combining the title and topic creates the searchable text.
  4. includes() checks for a keyword, and filter() collects matching notes.

An empty keyword matches all text, so the function returns all notes. This implementation supports simple title and topic search; it does not provide semantic matching or complex ranking.

Summary

Define the search scope, normalize the input, and filter the results. Small, explainable steps can produce a usable feature.