JS array toLocaleString()

🌙
手机阅读
本文目录结构

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

  1. var array1 = [1, 'a', new Date('21 Dec 1997 14:12:00 UTC')];
  2. var localeString = array1.toLocaleString('en', {timeZone: "UTC"});
  3. console.log(localeString);
  4. // expected output: "1,a,12/21/1997, 2:12:00 PM",
  5. // This assumes "en" locale and UTC timezone - your results may vary

语法

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

参数

locales 可选

带有BCP 47语言标记的字符串或字符串数组,关于locales参数的形式与解释,请看Intl页面。

options 可选

一个可配置属性的对象,对于数字 Number.prototype.toLocaleString(),对于日期Date.prototype.toLocaleString().

返回值

表示数组元素的字符串。

示例

使用localesoptions

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

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

  1. var prices = ['¥7', 500, 8123, 12];
  2. prices.toLocaleString('ja-JP', { style: 'currency', currency: 'JPY' });
  3. // "¥7,¥500,¥8,123,¥12"

更多实例请看 IntlNumberFormatDateTimeFormat页面。

Polyfill

  1. // https://tc39.github.io/ecma402/#sup-array.prototype.tolocalestring
  2. if (!Array.prototype.toLocaleString) {
  3. Object.defineProperty(Array.prototype, 'toLocaleString', {
  4. value: function(locales, options) {
  5. // 1. Let O be ? ToObject(this value).
  6. if (this == null) {
  7. throw new TypeError('"this" is null or not defined');
  8. }
  9. var a = Object(this);
  10. // 2. Let len be ? ToLength(? Get(A, "length")).
  11. var len = a.length >>> 0;
  12. // 3. Let separator be the String value for the
  13. // list-separator String appropriate for the
  14. // host environment's current locale (this is
  15. // derived in an implementation-defined way).
  16. // NOTE: In this case, we will use a comma
  17. var separator = ',';
  18. // 4. If len is zero, return the empty String.
  19. if (len === 0) {
  20. return '';
  21. }
  22. // 5. Let firstElement be ? Get(A, "0").
  23. var firstElement = a[0];
  24. // 6. If firstElement is undefined or null, then
  25. // a.Let R be the empty String.
  26. // 7. Else,
  27. // a. Let R be ?
  28. // ToString(?
  29. // Invoke(
  30. // firstElement,
  31. // "toLocaleString",
  32. // « locales, options »
  33. // )
  34. // )
  35. var r = firstElement == null ?
  36. '' : firstElement.toLocaleString(locales, options);
  37. // 8. Let k be 1.
  38. var k = 1;
  39. // 9. Repeat, while k < len
  40. while (k < len) {
  41. // a. Let S be a String value produced by
  42. // concatenating R and separator.
  43. var s = r + separator;
  44. // b. Let nextElement be ? Get(A, ToString(k)).
  45. var nextElement = a[k];
  46. // c. If nextElement is undefined or null, then
  47. // i. Let R be the empty String.
  48. // d. Else,
  49. // i. Let R be ?
  50. // ToString(?
  51. // Invoke(
  52. // nextElement,
  53. // "toLocaleString",
  54. // « locales, options »
  55. // )
  56. // )
  57. r = nextElement == null ?
  58. '' : nextElement.toLocaleString(locales, options);
  59. // e. Let R be a String value produced by
  60. // concatenating S and R.
  61. r = s + r;
  62. // f. Increase k by 1.
  63. k++;
  64. }
  65. // 10. Return R.
  66. return r;
  67. }
  68. });
  69. }

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

规范

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

参见

AXIHE / 精选资源

浏览全部教程

面试题

学习网站

前端培训
自己甄别

前端书籍

关于朱安邦

我叫 朱安邦,阿西河的站长,在杭州。

以前是一名平面设计师,后来开始接接触前端开发,主要研究前端技术中的JS方向。

业余时间我喜欢分享和交流自己的技术,欢迎大家关注我的 Bilibili

关注我: Github / 知乎

于2021年离开前端领域,目前重心放在研究区块链上面了

我叫朱安邦,阿西河的站长

目前在杭州从事区块链周边的开发工作,机械专业,以前从事平面设计工作。

2014年底脱产在老家自学6个月的前端技术,自学期间几乎从未出过家门,最终找到了满意的前端工作。更多>

于2021年离开前端领域,目前从事区块链方面工作了