JS-array sorting, js Array

Source: Internet
Author: User

JS-array sorting, js Array

<Script> // define an arr Array var arr = new Array (); // Add an arr Element a to the end of the Array. push ('A'); // obtain all array elements // alert (arr ); // a // obtain the first element in the array [starting from 0 by default] // alert (arr [0]); // a // obtain the length of the array // alert (arr. length); // 1 </script> <! ------------- I am a separation line ------------> <script> // shift: Delete the first entry of the original array and return the value of the deleted element ]; if the array is empty, return undefinedvar a = [1, 2, 3, 4, 5]; var B =. shift (); // alert (a); // a: [2, 3, 4, 5] // alert (B); // B: 1 </script> <! ------------- I am a separation line ------------> <script> // unshift: add the parameter to the beginning of the array, and return the [length] var a = [1, 2, 3, 4, 5] of the array; var B =. unshift (6, 7); // alert (a); // a: [6, 7, 1, 2, 3, 4, 5] // alert (B); // B: 7 </script> <! ------------- I am a separation line ------------> <script> // pop: Delete the last entry in the original array and return the value of the deleted element ], if the array is empty, return undefinedvar a = [1, 2, 3, 4, 5]; var B =. pop (); // alert (a); // a: [1, 2, 4] // alert (B); // B: 5 </script> <! ------------- I am a separation line ------------> <script> // push: add the parameter to the end of the original array, and return the [length] var a = [1, 2, 3, 4, 5] of the array; var B =. push (6, 7); // alert (a); // a: [1, 2, 3, 4, 5, 6, 7] // alert (B); // B: 7 </script> <! ------------- I am a separation line ------------> <script> // contact: returns a new array, which is a new array consisting of adding parameters to the original array var a = [1, 2, 3, 4, 5]; var B =. concat (); // alert (a); // a: [, 5] // alert (B); // B, 5, 6, 7] </script> <! ------------- I am a separation line ------------> <script> // splice (start, deleteCount, val1, val2 ,...): Delete the deleteCount item from the start position, and insert val1, val2 ,... var a = [1, 2, 3, 4, 5]; var B =. splice (2nd, 8); // Delete 2 items () from array a, and insert 6, 7, 8 // alert (a); // a: [1, 2, 6, 7, 8, 5] // alert (B); // B: [3, 4]/* ----------- I am also a separation line ---------- * // The Four usage of splice (same shift, unshift, pop, push) var a = [1, 2, 3, 4, 5]; var B =. splice (0, 1); // same as shift [Delete the first entry of the array] // Lert (a); // a: [2, 3, 4, 5] // alert (B); // 1/* ----------- I am also a separator line ------------ */var a = [1, 2, 3, 4, 5];. splice (0, 0,-2,-1); // same as unshift [add from the beginning of the array] var B =. length; // alert (a); // a: [-2,-,] // alert (B); // B: 7/* ----------- I am also a separation line ------------ */var a = [1, 2, 4, 5]; var B =. splice (. length-1, 1); // same as pop [Delete the last item] // alert (a); // a: [1, 2, 3, 4] // alert (B ); // B: 5/* ----------- I am also a separation line ------------ */var a = [1, 2, 4, 5];. splice (. lengt H, 7); // same as push [add from the end of the array] var B =. length; // alert (a); // a: [1, 2, 3, 4, 5, 6, 7] // alert (B); // B: 7 </script> <! ------------- I am a separation line ------------> <script> // reverse: returns the array reverse Order var a = [1, 2, 3, 4, 5]; var B =. reverse (); // alert (a); // a: [5, 4, 3, 2, 1] // alert (B); // B: [5, 4, 3, 2, 1] </script> <! ------------- I am a separation line ------------> <script> // sort (orderfunction): sorts the array according to the specified parameter var a = [,]; var B =. sort (); // alert (a); // a: [,] // alert (B); // B, 3, 33, 4, 5]/* ----------- I am also a separation line ------------ * // sort array a by the comparison function from small to large var a = [1, 2, 3, 4, 5, 33, 22, 11]; //. sort (new function () {'num1', 'num2', 'Return num1-num2 '}); // sort by character encoding order, 4, 5a. sort (function compare (num1, num2) {Return num1-num2;}); // sort from big to small: return num2-num1for (var I = 0; I <. length; I ++) {// document. write (a [I] + ''); // 1 2 3 4 5 11 22 33}/* ----------- I am also a separation line ------------ */var compare = function (num1, num2) {return num1-num2}; var a = [,];. sort (compare); for (var I = 0; I <. length; I ++) {document. write (a [I] + ''); // 1 2 3 4 5 11 22 33} </script> <! ------------- I am a separation line ------------> <script> // slice (start, end): returns the subscript specified from the original array to the end (excluding this element) a new array composed of items between var a = [1, 2, 3, 4, 5]; var B =. slice (2, 4); // alert (a); // a: [1, 2, 4, 5] // alert (B); // B: [3, 4] </script> <! ------------- I am a separator line ------------> <script> // join (separator): it is used to form an array element into a string with the separator, if this parameter is omitted, the default delimiter var a = [1, 2, 3, 4, 5] is used; // a: [1, 2, 3, 4, 5] var B =. join ('|'); // B: 1 | 2 | 3 | 4 | 5 // alert (a); // alert (B); </script>

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.