Intl.ListFormat.prototype.formatToParts() method returns an Array of objects representing the different components that can be used to format a list of values in a locale-aware fashion.

句法

Intl.ListFormat.prototype.formatToParts(list)
					

参数

list
Array of values to be formatted according to a locale.

返回值

Array of components which contains the formatted parts from the list.

描述

Whereas Intl.ListFormat.prototype.format() returns a string being the formated version of the list (according to the given locale and style options), formatToParts() returns an array of the different components of the formated string.

Each element of the resulting array has two properties: type and value type property may be either "element" , which refers to a value from the list, or "literal" which refers to a linguistic construct. The value property gives the content, as a string, of the token.

The locale and style options used for formatting are given when constructing the Intl.ListFormat 实例。

范例

Using formatToParts

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" }
// ]
					

规范

规范
Intl.ListFormat
The definition of 'formatToParts()' in that specification.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
formatToParts Chrome 72 Edge No Firefox 78 IE No Opera 60 Safari No WebView Android 72 Chrome Android 72 Firefox Android No Opera Android 51 Safari iOS No Samsung Internet Android No nodejs 12.0.0
12.0.0
Before version 13.0.0, only the locale data for en-US is available by default. See the ListFormat() 构造函数 了解更多细节。

图例

完整支持

完整支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

见实现注意事项。

另请参阅

元数据

  • 最后修改: