Array.prototype.toLocaleString()

toLocaleString() 返回一个字符串表示数组中的元素。数组中的元素将使用各自的 toLocaleString 方法转成字符串,这些字符串将使用一个特定语言环境的字符串(例如一个逗号 ",")隔开。

语法

arr.toLocaleString([locales[,options]]);

参数

locales 可选
带有BCP 47语言标记的字符串或字符串数组,关于locales参数的形式与解释,请看Intl页面。
options 可选
一个可配置属性的对象,对于数字 Number.prototype.toLocaleString(),对于日期Date.prototype.toLocaleString().

返回值

表示数组元素的字符串。

示例

使用localesoptions

数组中的元素将会使用各自的 toLocaleString 方法:

总是在prices数组中显示字符串和数字的货币符号:

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

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

更多实例请看 IntlNumberFormatDateTimeFormat页面。

Polyfill

// https://tc39.github.io/ecma402/#sup-array.prototype.tolocalestring
if (!Array.prototype.toLocaleString) {
  Object.defineProperty(Array.prototype, 'toLocaleString', {
    value: function(locales, options) {
      // 1. Let O be ? ToObject(this value).
      if (this == null) {
        throw new TypeError('"this" is null or not defined');
      }

      var a = Object(this);

      // 2. Let len be ? ToLength(? Get(A, "length")).
      var len = a.length >>> 0;

      // 3. Let separator be the String value for the 
      //    list-separator String appropriate for the 
      //    host environment's current locale (this is 
      //    derived in an implementation-defined way).
      // NOTE: In this case, we will use a comma
      var separator = ',';

      // 4. If len is zero, return the empty String.
      if (len === 0) {
        return '';
      }

      // 5. Let firstElement be ? Get(A, "0").
      var firstElement = a[0];
      // 6. If firstElement is undefined or null, then
      //  a.Let R be the empty String.
      // 7. Else,
      //  a. Let R be ? 
      //     ToString(? 
      //       Invoke(
      //        firstElement, 
      //        "toLocaleString", 
      //        « locales, options »
      //       )
      //     )
      var r = firstElement == null ? 
        '' : firstElement.toLocaleString(locales, options);

      // 8. Let k be 1.
      var k = 1;

      // 9. Repeat, while k < len
      while (k < len) {
        // a. Let S be a String value produced by 
        //   concatenating R and separator.
        var s = r + separator;

        // b. Let nextElement be ? Get(A, ToString(k)).
        var nextElement = a[k];

        // c. If nextElement is undefined or null, then
        //   i. Let R be the empty String.
        // d. Else,
        //   i. Let R be ? 
        //     ToString(? 
        //       Invoke(
        //        nextElement, 
        //        "toLocaleString", 
        //        « locales, options »
        //       )
        //     )
        r = nextElement == null ? 
          '' : nextElement.toLocaleString(locales, options);

        // e. Let R be a String value produced by 
        //   concatenating S and R.
        r = s + r;

        // f. Increase k by 1.
        k++;
      }

      // 10. Return R.
      return r;
    }
  });
}

如果你需要支持真正不支持Object.defineProperty的JavaScript引擎,最好不要对Array.prototype方法进行填充,因为你不能使它们不可枚举。

规范

Specification Status Comment
ECMAScript (ECMA-262)
Array.prototype.toLocaleString
Living Standard Initial definition was in ECMAScript 3.
ECMAScript Internationalization API (ECMA-402)
Array.prototype.toLocaleString
Living Standard This definition supersedes the definition provided in ECMA-262.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
toLocaleStringChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 5.5Opera Full support 4Safari Full support 1WebView Android Full support ≤37Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support 0.1.100
Optional locales parameterChrome Full support 24Edge Full support 79Firefox Full support 52IE No support NoOpera Full support 15Safari Full support 6.1WebView Android Full support ≤37Chrome Android Full support 25Firefox Android No support NoOpera Android Full support 14Safari iOS Full support 6.1Samsung Internet Android Full support 2.0nodejs Full support 13.0.0
Full support 13.0.0
Partial support 0.12
Notes
Notes Prior to Node.js 13.0.0, only the locale data for en-us is available by default. When other English locales are specified, the function silently falls back to en-us. When other languages are specified, it throws a RangeError. In order to make full ICU (locale) data available for versions prior to 13, see docs on the --with-full-icu= flag and how to provide the data.
Optional options parameterChrome Full support 24Edge Full support 79Firefox Full support 52IE No support NoOpera Full support 15Safari Full support 6.1WebView Android Full support ≤37Chrome Android Full support 25Firefox Android No support NoOpera Android Full support 14Safari iOS Full support 6.1Samsung Internet Android Full support 2.0nodejs Full support 0.12

Legend

Full support  
Full support
No support  
No support
See implementation notes.
See implementation notes.

参见