Intl.ListFormat
試してみましょう
Intl.ListFormat
オブジェクトは、言語特有のリストの整形を可能にします。
コンストラクター
Intl.ListFormat()
- 新しい
Intl.ListFormat
オブジェクトを作成します。
静的メソッド
Intl.ListFormat.supportedLocalesOf()
- 指定したロケールのうち、ランタイムの既定のロケールに代替されることなく対応しているものを含む配列を返します。
インスタンスメソッド
Intl.ListFormat.prototype.format()
- 言語固有の形式の文字列を返して,列挙要素を表示します。
Intl.ListFormat.prototype.formatToParts()
- ロケールを考慮した方法で値のリストを整形するために使用できる、さまざまな部分を表すオブジェクトの配列を返します。
例
format の使用
次の例では、英語を使用するリストのフォーマッターを生成する方法を示します。
const list = ['Motorcycle', 'Bus', 'Car'];
console.log(new Intl.ListFormat('en-GB', { style: 'long', type: 'conjunction' }).format(list));
// > Motorcycle, Bus and Car
console.log(new Intl.ListFormat('en-GB', { style: 'short', type: 'disjunction' }).format(list));
// > Motorcycle, Bus or Car
console.log(new Intl.ListFormat('en-GB', { style: 'narrow', type: 'unit' }).format(list));
// > Motorcycle Bus Car
formatToParts の使用
次の例では、整形済みの部分を返すリストフォーマッターを生成する方法を示します。
const list = ['Motorcycle', 'Bus', 'Car'];
console.log(new Intl.ListFormat('en-GB', { style: 'long', type: 'conjunction' }).formatToParts(list));
// [ { "type": "element", "value": "Motorcycle" },
// { "type": "literal", "value": ", " },
// { "type": "element", "value": "Bus" },
// { "type": "literal", "value": ", and " },
// { "type": "element", "value": "Car" } ];
ポリフィル
仕様書
ブラウザーの互換性
BCD tables only load in the browser