概述
Intl.ListFormat 根据语言规则将数组格式化为本地化串联文本,支持 type: 'conjunction'|'disjunction'|'unit' 与 style: 'long'|'short'|'narrow',适用于汇总与自然语言输出。
示例
const lf = new Intl.ListFormat('zh-CN', { type: 'conjunction', style: 'short' })
console.log(lf.format(['苹果', '香蕉', '梨'])) // 苹果、香蕉和梨
工程建议
- 文案一致:与其他 Intl 格式器协作;避免手工拼接导致语言不自然。
- 性能:缓存实例;避免在渲染循环中新建。
- 回退:不支持时使用 i18n 库或模板;确保标点与连词正确。
参考与验证
- MDN Intl.ListFormat 文档:https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat
- ECMA-402 规范:https://tc39.es/ecma402/#listformat-objects

发表评论 取消回复