A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/toLocaleString below:

Array.prototype.toLocaleString() - JavaScript | MDN

Array.prototype.toLocaleString()

Baseline Widely available

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
仕様書 ブラウザーの互換性 関連情報

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4