Pressidian
花园入口
笔记
项目
关于
实验室
GitHub
花园入口
笔记
项目
关于
实验室
GitHub

KNOWLEDGE PATHS

笔记库
当前位置
笔记库/前端/项目笔记/代达罗斯/踩坑

空字符串与filter

1 分钟阅读 · Note

目录树 578 篇

            • 踩坑
            • 空字符串与filter
            • 双周复盘:2026-07-03 至 2026-07-17
            • DataBase
            • drizzle的版本冲突
            • Schema化
          • 项目待做
          • 性能优化
          • UI设计
      • 前端技术栈
    • 笔记目录
    • CLAUDE.md
    • Vue 组件与 Render 函数

关联笔记 6

↗踩坑同一路径↗双周复盘:2026-07-03 至 2026-07-17同一路径↗DataBase同一路径↗drizzle的版本冲突同一路径↗Schema化同一路径↗「Feature」Introduce Crate as an independent metadata entity[ˈentəti] | 引入 Crate 作为独立元数据实体共同主题
  • 空字符串与filter

空字符串与filter

> Last Format Time:7/14/2026 20:25:30

最后这句:

.filter(Boolean);

是为了去掉空字符串。

你的代码整体逻辑是:

const parseCommaList = (s: string): string[] =>
    s
        .split(",")          // 按逗号分割
        .map(item => item.trim()) // 去掉每项首尾空格
        .filter(Boolean);    // 去掉空字符串

举几个例子就很容易理解。

正常情况

parseCommaList("a,b,c")

执行过程:

split(",")
// ["a", "b", "c"]

trim()
// ["a", "b", "c"]

filter(Boolean)
// ["a", "b", "c"]

没有变化。

有空格

parseCommaList("a, b, c ")

执行:

split(",")
// ["a", " b", " c "]

trim()
// ["a", "b", "c"]

filter(Boolean)
// ["a", "b", "c"]

连续两个逗号

parseCommaList("a,,c")

执行:

split(",")
// ["a", "", "c"]

trim()
// ["a", "", "c"]

filter(Boolean)
// ["a", "c"]

如果没有 filter(Boolean),结果就是:

["a", "", "c"]

中间会多一个空字符串。

首尾有逗号

parseCommaList(",a,b,")

执行:

const parseCommaList = (s: string): string[] =>
    s
        .split(",")          // 按逗号分割
        .map(item => item.trim()) // 去掉每项首尾空格
        .filter(Boolean);    // 去掉空字符串

全都是空

const parseCommaList = (s: string): string[] =>
    s
        .split(",")          // 按逗号分割
        .map(item => item.trim()) // 去掉每项首尾空格
        .filter(Boolean);    // 去掉空字符串

执行:

const parseCommaList = (s: string): string[] =>
    s
        .split(",")          // 按逗号分割
        .map(item => item.trim()) // 去掉每项首尾空格
        .filter(Boolean);    // 去掉空字符串

为什么 filter(Boolean) 能过滤空字符串?

Array.prototype.filter 需要一个返回 true/false 的函数:

const parseCommaList = (s: string): string[] =>
    s
        .split(",")          // 按逗号分割
        .map(item => item.trim()) // 去掉每项首尾空格
        .filter(Boolean);    // 去掉空字符串

实际上就是下面的简写:

const parseCommaList = (s: string): string[] =>
    s
        .split(",")          // 按逗号分割
        .map(item => item.trim()) // 去掉每项首尾空格
        .filter(Boolean);    // 去掉空字符串

而 JavaScript 中:

const parseCommaList = (s: string): string[] =>
    s
        .split(",")          // 按逗号分割
        .map(item => item.trim()) // 去掉每项首尾空格
        .filter(Boolean);    // 去掉空字符串

由于前面已经执行了 trim(),所以 " " 会变成 "",于是:

const parseCommaList = (s: string): string[] =>
    s
        .split(",")          // 按逗号分割
        .map(item => item.trim()) // 去掉每项首尾空格
        .filter(Boolean);    // 去掉空字符串

所以这段代码可以理解成:

const parseCommaList = (s: string): string[] =>
    s
        .split(",")          // 按逗号分割
        .map(item => item.trim()) // 去掉每项首尾空格
        .filter(Boolean);    // 去掉空字符串

两种写法效果一样,只是 filter(Boolean) 更简洁,也是 JavaScript/TypeScript 中非常常见的一种惯用写法。