Some operations on JavaScript arrays (2)

Source: Internet
Author: User

7. reverse the order of elements in the array (Note: Not to sort the array) -- reverse method

 Reversing the order of elements in an array<Script type = "text/javascript"> var intArray = [1, 5, 3, 0]; intArray. reverse (); for (var intIndex in intArray) {console. log (intArray [intIndex]) ;}</script>
Note: for loop output in sequence: 0 3 5 1;

8. sort Arrays-sort method

Note: This method can pass the name of a custom sorting method, if no parameter is specified, the array is sorted alphabetically in ascending order (I .e., in ascending order of character encoding). If you want to sort numbers, the following code shows the "unexpected" effect:

 Sort Arrays-Sort letters and characters<Script type = "text/javascript"> var charArray = ["a", "z", "g", "r"]; charArray. sort (); for (var charIndex in charArray) {console. log (charArray [charIndex]) ;}</script>
Note: for loops are output in sequence: a g r z;
 Sorting of Arrays-sorting of numbers<Script type = "text/javascript"> var intArray = ["10", "5", "40", "25", "1000", "1"]; intArray. sort (); for (var intIndex in intArray) {console. log (intArray [intIndex]) ;}</script>
Note: for loop output in sequence: 1 10 1000 25 40 5;
Summary: compared with the above two pieces of code, it is not difficult to find that the sort method without parameters does not have the sorting effect when sorting arrays of the number type (hehahaha, someone may think: if I change the number array element in the code above to "var intArray = [10, 5, 40, 25,100 0, 1];", you should be able to do it, this is not the case if you want to sort the number array:

 Sorting of Arrays-sorting of numbers<Script type = "text/javascript"> function compareTo (a, B) {return a-B;} var intArray = ["10", "5", "40 ", "25", "1000", "1"]; intArray. sort (compareTo); for (var intIndex in intArray) {console. log (intArray [intIndex]) ;}</script>
Or
 Sorting of Arrays-sorting of numbers<Script type = "text/javascript"> var intArray = ["10", "5", "40", "25", "1000", "1"]; intArray. sort (function (a, B) {return a-B;}); for (var intIndex in intArray) {console. log (intArray [intIndex]) ;}</script>
Note: for loop output in sequence: 1 5 10 25 40 1000;

Description of comparison function parameters: If you want to sort by custom sorting rules, you need to provide a comparison function. This function requires two parameters (usually named a and B) return a number indicating the relative sequence of the two values. The return value is as follows:

1) if a is smaller than B, a value smaller than 0 will be returned if a appears before B in the sorted array.

2) if a is equal to B, 0 is returned.

3) if a is greater than B, a value greater than 0 is returned if a appears after B in the sorted array.

I do not know whether you have noticed the sorting of Chinese characters for the array's preface. I have explained the sorting of letters and numbers. How about sort's sorting of Chinese characters?

 Sorting of arrays -- sorting of Chinese Characters<Script type = "text/javascript"> var charArray = ["medium", "Ah", "home", "high"]; charArray. sort (); for (var charIndex in charArray) {console. log (charArray [charIndex]) ;}</script>

Or

 Sorting of arrays -- sorting of Chinese Characters<Script type = "text/javascript"> var charArray = ["medium", "Ah", "home", "high"]; charArray. sort (function (a, B) {return a-B;}); for (var charIndex in charArray) {console. log (charArray [charIndex]) ;}</script>

Note: The results of the for loop output in the above two code snippets are the same-output one by one: Center, home, and height;

Conclusion: From the output result, we can find that the above Code is wrong in sorting Chinese. For how to sort Chinese, see the following code:

 Sorting of arrays -- sorting of Chinese Characters<Script type = "text/javascript"> var charArray = ["medium", "Ah", "home", "high"]; charArray. sort (function (a, B) {return. localeCompare (B) ;}); for (var charIndex in charArray) {console. log (charArray [charIndex]) ;}</script>
Note: The above for loop is output in sequence: Ah Gao Jia Zhong; this is the result we want.

Summary: If you want to sort Chinese characters, you still need to use the sort method, but the localeCompare method is used in the sort comparison method, this method also returns a number indicating the relative sequence of the two values. The return value is as follows:

1) if a is smaller than B, a value smaller than 0 will be returned if a appears before B in the sorted array.

2) if a is equal to B, 0 is returned.

3) if a is greater than B, a value greater than 0 is returned if a appears after B in the sorted array.

9. Convert the elements in the array to a string separated by specific characters-join method

 Convert the elements in the array to a string separated by specific characters-separated by commas<Script type = "text/javascript"> var nameArray = ["Chen Hongtao", "Li xiaoning", "Wu Siwei"]; var nameString = nameArray. join (); console. log (nameString); </script>
Note: The console outputs the following results: Chen Hongtao, Li xiaoning, and Wu Siyu.

 Converts an element in an array to a string separated by a specific character.<Script type = "text/javascript"> var nameArray = ["Chen Hongtao", "Li xiaoning", "Wu Siwei"]; var nameString = nameArray. join (","); console. log (nameString); </script>
Note: The console outputs the following results: Chen Hongtao, Li xiaoning, and Wu Siyu.

Conclusion: With the above two pieces of code, we can further understand the join method. If no parameters are passed for this method, the default array element Delimiter is a comma; if a parameter is passed for this method, the array element Delimiter is the delimiter specified by the passed parameter;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.