Home >Web Front-end >JS Tutorial >How to use string to represent elements in array in js
In the previous article, we learned about the method of determining whether an element in an array passes the test. Please see "How does js determine whether an element in an array passes the test". This time we will learn how to use strings to represent elements in an array. You can refer to it if you need it.
Previously introduced the method of using join to convert the elements in the array into a string and the method of using tostring to convert the elements in the array into a string . This time, Let's talk about another way to convert array elements into strings. This method is toLocaleString().
First let’s look at a small case.
<script> var arr = new Array(3); arr[0] = "one"; arr[1] = "two"; arr[2] = "three"; var nums = arr.toLocaleString(); console.log(nums); console.log(arr); </script>
The result of this small example is
Let’s look at the result. The first one is a string. Two is an array, and the contents in the two are exactly the same, both are "one, two, three". Let’s look at the code again. The first one is where we use the toLocaleString() method, and the second one is our original array. After knowing a little bit about the toLocaleString() method, let's study this method in detail.
toLocaleString()
Returns a string representing the element in the array. The elements in the array will be converted to strings using their respective toLocaleString methods, and these strings will be separated by a locale-specific string (such as a comma ",
"). It can convert a number object into a string in local format, or convert a date object into a string according to local time.
Let’s take a look at the syntax format of this method.
数组名称.toLocaleString(数组,一个可配置属性的对象);
It should be noted that the first parameter of this method must be a string or string array with BCP 47 language tag.
That’s all. If you need it, you can read: javascript advanced tutorial
The above is the detailed content of How to use string to represent elements in array in js. For more information, please follow other related articles on the PHP Chinese website!