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
实例。
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. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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
|
完整支持
不支持
实验。期望将来行为有所改变。
见实现注意事项。
Intl.ListFormat
Intl.ListFormat.prototype.format()
Intl.RelativeTimeFormat.prototype.formatToParts()
Intl.NumberFormat.prototype.formatToParts()
Intl.DateTimeFormat.prototype.formatToParts()
Intl
Collator
DateTimeFormat
ListFormat
NumberFormat
PluralRules
RelativeTimeFormat
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()