Array.prototype.toLocaleString()

Baseline Widely available

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

Array 实例的 toLocaleString() 方法返回一个表示数组元素的字符串。使用 toLocaleString 方法将元素转换为字符串,并且这些字符串由特定于区域设置的字符串(例如逗号 ",")分隔。

¥The toLocaleString() method of Array instances returns a string representing the elements of the array. The elements are converted to strings using their toLocaleString methods and these strings are separated by a locale-specific string (such as a comma ",").

Try it

语法

¥Syntax

js
toLocaleString()
toLocaleString(locales)
toLocaleString(locales, options)

参数

¥Parameters

locales Optional

带有 BCP 47 语言标记的字符串,或此类字符串的数组。有关 locales 论证的一般形式和解释,请参阅 Intl 主页参数说明

options Optional

具有配置属性的对象。数字见 Number.prototype.toLocaleString();日期见 Date.prototype.toLocaleString()

返回值

¥Return value

表示数组元素的字符串。

¥A string representing the elements of the array.

描述

¥Description

Array.prototype.toLocaleString 方法遍历其内容,使用提供的 localesoptions 参数调用每个元素的 toLocaleString 方法,并将它们与实现定义的分隔符(例如逗号 ",")连接起来。请注意,该方法本身不消耗这两个参数 - 它仅将它们传递给每个元素的 toLocaleString()。分隔符字符串的选择取决于主机当前的区域设置,而不是 locales 参数。

¥The Array.prototype.toLocaleString method traverses its content, calling the toLocaleString method of every element with the locales and options parameters provided, and concatenates them with an implementation-defined separator (such as a comma ","). Note that the method itself does not consume the two parameters — it only passes them to the toLocaleString() of each element. The choice of the separator string depends on the host's current locale, not the locales parameter.

如果某个元素是 undefinednull,则将其转换为空字符串,而不是字符串 "null""undefined"

¥If an element is undefined, null, it is converted to an empty string instead of the string "null" or "undefined".

当在 稀疏数组 上使用时,toLocaleString() 方法会迭代空槽,就好像它们具有值 undefined 一样。

¥When used on sparse arrays, the toLocaleString() method iterates empty slots as if they have the value undefined.

toLocaleString() 方法是 generic。它只期望 this 值具有 length 属性和整数键控属性。

¥The toLocaleString() method is generic. It only expects the this value to have a length property and integer-keyed properties.

示例

¥Examples

使用区域设置和选项

¥Using locales and options

使用 toLocaleString 方法将数组的元素转换为字符串。

¥The elements of the array are converted to strings using their toLocaleString methods.

始终显示 prices 数组中字符串和数字的货币:

¥Always display the currency for the strings and numbers in the prices array:

js
const prices = ["¥7", 500, 8123, 12];
prices.toLocaleString("ja-JP", { style: "currency", currency: "JPY" });

// "¥7,¥500,¥8,123,¥12"

有关更多示例,另请参阅 Intl.NumberFormatIntl.DateTimeFormat 页。

¥For more examples, see also the Intl.NumberFormat and Intl.DateTimeFormat pages.

在稀疏数组上使用 toLocaleString()

¥Using toLocaleString() on sparse arrays

toLocaleString() 对待空槽的方式与 undefined 相同,并产生一个额外的分隔符:

¥toLocaleString() treats empty slots the same as undefined and produces an extra separator:

js
console.log([1, , 3].toLocaleString()); // '1,,3'

对非数组对象调用 toLocaleString()

¥Calling toLocaleString() on non-array objects

toLocaleString() 方法读取 thislength 属性,然后访问键为小于 length 的非负整数的每个属性。

¥The toLocaleString() method reads the length property of this and then accesses each property whose key is a nonnegative integer less than length.

js
const arrayLike = {
  length: 3,
  0: 1,
  1: 2,
  2: 3,
  3: 4, // ignored by toLocaleString() since length is 3
};
console.log(Array.prototype.toLocaleString.call(arrayLike));
// 1,2,3

规范

Specification
ECMAScript Language Specification
# sec-array.prototype.tolocalestring
ECMAScript Internationalization API Specification
# sup-array.prototype.tolocalestring

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看