Intl.ListFormat.prototype.formatToParts()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2021.

Intl.ListFormat.prototype.formatToParts() メソッドは、ロケールを考慮した値のリストの書式化で使用できる様々な部分を表すオブジェクトの配列 (Array) を返します。

試してみましょう

const vehicles = ["Motorcycle", "Bus", "Car"];

const formatterEn = new Intl.ListFormat("en", {
  style: "long",
  type: "conjunction",
});

const formatterFr = new Intl.ListFormat("fr", {
  style: "long",
  type: "conjunction",
});

const partValuesEn = formatterEn.formatToParts(vehicles).map((p) => p.value);
const partValuesFr = formatterFr.formatToParts(vehicles).map((p) => p.value);

console.log(partValuesEn);
// Expected output: "["Motorcycle", ", ", "Bus", ", and ", "Car"]"
console.log(partValuesFr);
// Expected output: "["Motorcycle", ", ", "Bus", " et ", "Car"]"

構文

js
formatToParts(list);

引数

list

ロケールに従って書式化する反復可能オブジェクト、例えば配列 (Array) です。

返値

リストから書式された部品を含むコンポーネントの配列 (Array) です。

解説

Intl.ListFormat.prototype.format() が、(渡されたロケールとスタイルのオプションに応じて)リストの書式化された文字列を返すのに対し、 formatToParts() は、書式化されたされた文字列のさまざまなコンポーネントの配列を返します。

結果として得られる配列の各要素には、 typevalue の 2 つのプロパティがあります。 type プロパティはリストの値を指す "element" か、言語的な構成要素を指す "literal" かのどちらかです。 value プロパティはトークンの内容を文字列で指定します。

書式化に使用されるロケールとスタイルのオプションは、 Intl.ListFormat インスタンスを構築する際に与えられたものです。

formatToParts の使用

js
const fruits = ["Apple", "Orange", "Pineapple"];
const myListFormat = new Intl.ListFormat("en-GB", {
  style: "long",
  type: "conjunction",
});

console.table(myListFormat.formatToParts(fruits));
// [
//  { "type": "element", "value": "Apple" },
//  { "type": "literal", "value": ", " },
//  { "type": "element", "value": "Orange" },
//  { "type": "literal", "value": ", and " },
//  { "type": "element", "value": "Pineapple" }
// ]

仕様書

Specification
ECMAScript® 2025 Internationalization API Specification
# sec-Intl.ListFormat.prototype.formatToParts

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
formatToParts

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
See implementation notes.

関連情報