In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to set array to string in jquery". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "jquery how to set the array to string" it!
Methods: 1, the use of toString () method, the method can convert the array into a string, syntax for the "array element .toString ()"; 2, the use of join () method, the method can be connected to the array elements to build a string, syntax for "array element .join (separator)".
The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.
How to convert an array to a string by jquery
Jquery allows arrays to be converted to and from strings. The Array method object defines three methods that can convert an array to a string, as shown in the table.
Example 1
Let's use the toString () method to read the values of the array.
The toString () method in the array converts each element to a string and then displays it as a comma concatenated output.
Var a = [1 typeof 2, 3, 5, 7, 7, 7, 7, and 7]; / / defines the array var s = a.toString (); / / converts the 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, you need to explicitly call this method.
Var a = [1, var 2, 3, 5, 6, 7, 7, 7, and 9]; / define the array var b = [1, 2, 3, 4, 5, 7, 7, 6, 7, 6, 6, 5, 6, 5, 6, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 9, 9, 9, 6, 6, 6, 6, 9, 9, 9, 0, 6, 6, 9, 9, 9, 0, console.log (typeof s); / / returns a string string, indicating that it is a 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, the strings are delimited by commas and output as a list.
Var a = [1, [2jue 3], [4jre 5]], [6, [7, [8 Magi 9], 0]; / / define the multidimensional array var s = a.toString (); / / convert the array to the string console.log (S); / / return the string "1line 2p3pm 4M0"
The array an is a multi-dimensional array, and JavaScript iteratively calls the toString () method to convert all arrays to strings.
Example 2
Let's use the toLocalString () method to read the values of the array.
The use of the toLocalString () method is basically the same as that of the toString () method, except that the toLocalString () method can concatenate the resulting string using a delimiter specific to the user's locale to form a string.
Var a = [1 var s = a.toLocalString (); / / convert the array to the local string console.log (s); / / return the string "1, 2, 4, 5, 7, 7, 9, 0"
In the above example, the toLocalString () method converts a number to a floating-point number before performing a string conversion operation, according to Chinese usage.
Example 3
Below, you can convert an array to a string using the join () method.
The join () method can convert 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 connect each element. If the parameter is omitted, a comma is used as the delimiter by default, which is the same as the conversion operation of the toString () method.
Var a = [1, 2, 4, 5]; / define the array var s = a.join ("= ="); / / specify the delimiter console.log (s); / / return the string "1, 2, 4, 4, 5".
Example 4
Let's use the split () method to convert the string to an array.
The split () method is a String object method, which operates the opposite of the join () method. This method can specify two parameters, the first parameter is a delimiter that specifies where to separate the tag, and the second parameter specifies the length of the array to return.
Var s = "1 strings 2 strings = 3 strings 4 = = 5"; var a = s.split ("= ="); console.log (a); console.log (a.constructor = = Array). At this point, I believe you have a better understanding of "how to set arrays to strings". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.