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 2015å¹´7æ.
toLocaleString() 㯠Array ã¤ã³ã¹ã¿ã³ã¹ã®ã¡ã½ããã§ãé
åã®è¦ç´ ã表ãæååãè¿ãã¾ããé
åã®è¦ç´ ã¯ãããããã® toLocaleString ã¡ã½ããã使ãããã±ã¼ã«åºæã®æååã«å¤æããã¾ãï¼ä¾ãã°ã«ã³ã "," ãªã©ï¼ã
試ãã¦ã¿ã¾ããã
const array = [1, "a", new Date("21 Dec 1997 14:12:00 UTC")];
const localeString = array.toLocaleString("en", { timeZone: "UTC" });
console.log(localeString);
// äºæ³ãããçµæ: "1,a,12/21/1997, 2:12:00 PM",
// "en" ãã±ã¼ã«ããã³ UTC ã¿ã¤ã ã¾ã¼ã³ãæ³å®ãããã®ãçµæã¯ç°ãªããã¨ããã
æ§æ
toLocaleString()
toLocaleString(locales)
toLocaleString(locales, options)
弿°
localesçç¥å¯-
BCP 47 è¨èªã¿ã°ã®æååãããã®é åã§ãã
locales弿°ã®ä¸è¬çãªå½¢å¼ã¨è§£éã«ã¤ãã¦ã¯ãIntlã¡ã¤ã³ãã¼ã¸ã®å¼æ°ã®èª¬æãåç §ãã¦ãã ããã optionsçç¥å¯-
è¨å®ããããã£ã®ãªãã¸ã§ã¯ãã§ããããã§æ¸¡ããã¨ãã§ãããã®ã¯ã夿ãããè¦ç´ ã«ãã£ã¦ç°ãªãã¾ããä¾ãã°ãæ°å¤ã®å ´åã¯
Number.prototype.toLocaleString()ãåç §ãã¦ãã ããã
è¿å¤
é åã®è¦ç´ ã表ãæååã§ãã
解説
Array.prototype.toLocaleString ã¡ã½ããã¯ããã®å
容ãèµ°æ»ãããã¹ã¦ã®è¦ç´ ã«å¯¾ã㦠toLocaleString ã¡ã½ãããã弿° locales 㨠options ãæå®ãã¦å¼ã³åºããå®è£
ã§å®ç¾©ãããåºåãæåï¼ä¾ãã°ã«ã³ã ","ï¼ã§ãã®çµæãé£çµãããã®ãè¿ãã¾ãã
ã¡ã¢:
locales ã¾ã㯠options 弿°ã¯ãé
åè¦ç´ éã®åºåãæåãå¶å¾¡ãããã®ã§ã¯ããã¾ããããããã¯ãããããã®è¦ç´ ã® toLocaleString() ã¡ã½ããã«æ¸¡ãããã ãã§ããå®éã®åºåãæåï¼é常ã¯ã«ã³ãï¼ã¯ããã¹ãã®ç¾å¨ã®ãã±ã¼ã«ã«ã®ã¿ä¾åãã¾ãããã¼ã«ã©ã¤ãºããããªã¹ãã®æ¸å¼åãå¿
è¦ãªå ´åã¯ã代ããã« Intl.ListFormat ã使ç¨ãããã¨ãæ¤è¨ãã¦ãã ããã
è¦ç´ ã undefinedãnull ã®å ´åãæåå "null" ã¾ã㯠"undefined" ã®ä»£ããã«ç©ºæååã«å¤æããã¾ãã
çé
åã§ä½¿ç¨ããå ´åãtoLocaleString() ã¡ã½ããã¯ç©ºã®ã¹ãããã undefined ã¨ããå¤ããããã®ããã«å復å¦çãã¾ãã
toLocaleString() ã¡ã½ããã¯æ±ç¨çã§ãããã®ã¡ã½ãã㯠this å¤ã« length ããããã£ã¨æ´æ°ãã¼ã®ããããã£ããããã¨ã ããæå¾
ãã¾ãã
ä¾
>locales 㨠options ã®ä½¿ç¨
é
åã®è¦ç´ ã¯ããã® toLocaleString ã¡ã½ããã使ç¨ãã¦æååã«å¤æããã¾ããä¾ãã°ããã®ã¹ããããã¯ãprices é
åã®æååããã³æ°å¤ã®é貨ã表示ããããã«ãNumber.prototype.toLocaleString() ã¡ã½ãããæé»çã«å¼ã³åºãã¾ãã
const prices = ["ï¿¥7", 500, 8123, 12];
prices.toLocaleString("ja-JP", { style: "currency", currency: "JPY" });
// "ï¿¥7,ï¿¥500,ï¿¥8,123,ï¿¥12"
ãªã¹ãã®åºåãæå
ãªã¹ãã®åºåãæåã¯ãlocales 弿°ã®å½±é¿ãåãã¾ããããããè¨å®ããã«ã¯ã代ããã« Intl.ListFormat ã使ç¨ãã¦ãã ããã
const nums = [8888, 9999];
console.log(nums.toLocaleString("zh")); // "8,888,9,999"
const formatter = new Intl.ListFormat("zh", {
type: "conjunction",
style: "narrow",
});
console.log(formatter.format(nums.map((x) => x.toLocaleString("zh"))));
// "8,888ã9,999"
çé åã«å¯¾ãã toLocaleString() ã®ä½¿ç¨
toLocaleString() ã¯ç©ºã®ã¹ãããã undefined ã¨åãããã«æ±ããåºåãæåã追å ãã¾ãã
console.log([1, , 3].toLocaleString()); // '1,,3'
é å以å¤ã®ãªãã¸ã§ã¯ãã«å¯¾ãã toLocaleString() ã®å¼ã³åºã
toLocaleString() ã¡ã½ãã㯠this ã® length ããããã£ãèªã¿è¾¼ã¿ããã®ãã¼ã length ãããå°ããéè² ã®æ´æ°ã§ããåããããã£ã«ã¢ã¯ã»ã¹ãã¾ãã
const arrayLike = {
length: 3,
0: 1,
1: 2,
2: 3,
3: 4, // length ã 3 ã§ãããã toLocaleString() ããã¯ç¡è¦ããã
};
console.log(Array.prototype.toLocaleString.call(arrayLike));
// 1,2,3
仿§æ¸
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-array.prototype.tolocalestring> |
| ECMAScript® 2027 Internationalization API Specification> # sup-array.prototype.tolocalestring> |