Analysis of JS Array to String Realization Method

  • 2021-08-10 06:47:44
  • OfStack

JavaScript allows conversions between arrays and strings. The Array method object defines three methods that convert arrays to strings, as shown in the table.

数组方法 说明
toString() 将数组转换成1个字符串
toLocalString() 把数组转换成本地约定的字符串
join() 将数组元素连接起来以构建1个字符串

Array object array and string conversion method Array method description toString () converts an array into a string toLocalString () converts an array into a locally agreed string join () concatenates array elements to build a string

Example 1 toString ()

The value of the array is read using the toString () method.

The toString () method in the array converts each element to a string, and the output is displayed as a comma concatenation.

var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]; //Define arrays
var s = a. toString (); //Convert an array to a string
console. log (s); //Returns the string "1, 2, 3, 4, 5, 6, 7, 8, 9, 0"
console. log (typeof s); //Returns the string string, indicating that it is a string type

When an array is used in a string environment, JavaScript automatically calls the toString () method to convert the array to a string. In some cases, this method needs to be explicitly called.

var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]; //Define arrays
var b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]; //Define arrays
var s = a + b; //Array join operation
console. log (s); //Returns "1, 2, 3, 4, 5, 6, 7, 8, 9, 01, 2, 3, 4, 5, 6, 7, 8, 9, 0"
console. log (typeof s); //Returns the string string, indicating that it is of string type

When toString () converts an array to a string, it first converts each element of the array to a string. When each element is converted to a string, it is separated by commas, and these strings are output in the form of a list.

var a = [1, [2, 3], [4, 5]], [6, [7, [8, 9], 0]]; //Define multidimensional arrays
var s = a. toString (); //Convert an array to a string
console. log (S); //Returns the string "1, 2, 3, 4, 5, 6, 7, 8, 9, 0"

Where the array a is a multidimensional array, JavaScript iteratively calls the toString () method to convert all arrays to strings.

Example 2 toLocaleString ()

The value of the array is read using the toLocaleString () method.

The toLocaleString () method is basically the same as the toString () method, with the main difference that the toLocaleString () method can concatenate the generated strings using the user's locale-specific delimiter to form a string.

var a = [1, 2, 3, 4, 5]; //Define arrays
var s = a. toLocaleString (); //Convert an array to a local string
console. log (s); //Returns the string "1, 2, 3, 4, 5, 6, 7, 8, 9, 0"

In the above example, the toLocaleString () method converts numbers to floating-point numbers before performing string conversion operations, according to Chinese usage habits.

Example 3 join ()

The array can be converted to a string using the join () method below.

The join () method converts an array to a string, but it can specify a delimiter. When you call the join () method, you can pass a parameter as a delimiter to join each element. If the parameter is omitted, the comma is used as the delimiter by default, which has the same effect as the toString () method conversion operation.

var a = [1, 2, 3, 4, 5]; //Define arrays
var s = a. join ("=="); //Specify the separator
console. log (s); //Returns the string "1==2==3==4==5"

Example 4 split ()

Let's use the split () method to convert strings into arrays.

The split () method is an String object method, which is the opposite of the join () method operation. This method can specify two parameters, the first parameter is the delimiter, specifying where to delimit the mark; The second parameter specifies the length of the array to be returned.

var s = "1==2== 3==4 ==5";
var a = s.split("==");
console.log(a);
console.log(a.constructor == Array);


Related articles: