Briefly
The method to
returns the representation of an array as a string consisting of the results of converting each element of the array to a string according to the type of element and the passed localization parameters. The value separator is the character defined according to the localization parameters of the runtime environment (,
or another used for separating elements in a list).
For array elements that are null
or undefined
, an empty string is returned.
Example
Let's get a string from the array considering the localization de
(German language and regional standards for numbers and dates in Germany):
const array = [null, 50, 15000, new Date('2007-08-10')]const arrayStr = array.toLocaleString('de-DE')console.log(arrayStr)// 50,15.000,10.8.2007, 03:00:00
const array = [null, 50, 15000, new Date('2007-08-10')] const arrayStr = array.toLocaleString('de-DE') console.log(arrayStr) // 50,15.000,10.8.2007, 03:00:00
Let's get a string from the same array considering the localization en
(English language and regional standards for numbers and dates in the USA) and specifying the time zone:
const array = [null, 50, 15000, new Date('2007-08-10')]const arrayStr = array.toLocaleString( 'en-US', {timeZone: 'Europe/Amsterdam'})console.log(arrayStr)// 50,15,000,8/10/2007, 2:00:00 AM
const array = [null, 50, 15000, new Date('2007-08-10')] const arrayStr = array.toLocaleString( 'en-US', {timeZone: 'Europe/Amsterdam'} ) console.log(arrayStr) // 50,15,000,8/10/2007, 2:00:00 AM
How it works
Array
takes two optional arguments:
locales
— a string that defines the locale (information about the user's language and regional settings) or an array of such strings;options
— a configuration object used when calling theto
method on each element of the array.Locale String ( )
Array
returns a string consisting of the results of invoking the to
method for all elements of the array, separated by a comma or another separator character.
If the method is called without arguments, the localization parameters set in the runtime environment will be used.
How to understand it
The to
method is intended to represent data according to the specified localization.
Localization is needed for numbers, dates, and currency units, as different regional standards use various formats for presenting this data. For example, the number 1000 can be represented as a string in different ways:
1
—, 000 en
;- U S 1 000
—ru
;- R U 1
—. 000 de
.- D E
The to
method is defined in the prototype Object
and is inherited by all built-in objects.
In addition to Object
and Array
, built-in objects have their own implementations of the to
method:
Number
;Date
;Big
;Int Typed
.Array
Traversing the array, the to
method invokes to
for each element, according to its type (if the value is not null
or undefined
) and combines the result into a string, using the established separator (usually a comma).
If an array element is a primitive type, the value is wrapped in the corresponding wrapper object to apply the to
method to it.
All primitive type values except null
and undefined
have a corresponding wrapper object. For example: true — Boolean, 175 — Number, 'Aquarium' — String.
The array method to
is a convenient way to obtain a string from an array of data considering the localization parameters.
Let's get a string of prices in euros from an array of numbers:
const numbers = [150, 20, 800, 0]const pricesStr = numbers.toLocaleString( 'de-DE', {style: 'currency', currency: "EUR"})console.log(pricesStr)// 150,00 €,20,00 €,800,00 €,0,00 €
const numbers = [150, 20, 800, 0] const pricesStr = numbers.toLocaleString( 'de-DE', {style: 'currency', currency: "EUR"} ) console.log(pricesStr) // 150,00 €,20,00 €,800,00 €,0,00 €