Intl.ListFormat.prototype.format() 方法是 JavaScript 中的一個內置方法,它返回一個帶有列表的 language-specific 表示的字符串。
用法:
listFormat.format([list]);
參數:此方法接受上述和以下描述的單個參數:
- list:此參數包含一個可迭代對象,例如一個 Array。
返回值:此方法返回一個 language-specific 格式的字符串,表示列表的元素。
下麵的例子說明了 JavaScript 中的 Intl.ListFormat.prototype.format() 方法:
範例1:
<script>
const gfg = ['Geeks1', 'Geeks2', 'Geeks3', 'Geeks4'];
const result1 = new Intl.ListFormat('en',
{ style:'long', type:'conjunction' });
console.log(result1.format(gfg));
const result2 = new Intl.ListFormat('db',
{ style:'short', type:'disjunction' });
console.log(result2.format(gfg));
const result3 = new Intl.ListFormat('en',
{ style:'narrow', type:'unit' });
console.log(result3.format(gfg));
</script>
輸出:
"Geeks1, Geeks2, Geeks3, and Geeks4" "Geeks1, Geeks2, Geeks3, or Geeks4" "Geeks1 Geeks2 Geeks3 Geeks4"
範例2:
<script>
const gfg = ['Geeks1', 'Geeks2', 'Geeks3', 'Geeks4'];
const result1 = new Intl.ListFormat('hi',
{ style:'long', type:'conjunction' });
console.log(result1.format(gfg));
const result2 = new Intl.ListFormat('hi',
{ style:'short', type:'disjunction' });
console.log(result2.format(gfg));
const result3 = new Intl.ListFormat('hi',
{ style:'narrow', type:'unit' });
console.log(result3.format(gfg));
</script>
輸出:
支持的瀏覽器: Intl.ListFormat.prototype.format() 方法支持的瀏覽器如下:
- 穀歌瀏覽器
- Opera
相關用法
- Javascript dataView.getInt16()用法及代碼示例
- Javascript RegExp toString()用法及代碼示例
- JavaScript Date toLocaleTimeString()用法及代碼示例
- JavaScript Math random()用法及代碼示例
- JavaScript Math round()用法及代碼示例
- Javascript toFixed()用法及代碼示例
注:本文由純淨天空篩選整理自SHUBHAMSINGH10大神的英文原創作品 JavaScript | Intl.ListFormat.prototype.format() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。