Common Methods for Javascript array objects

Source: Internet
Author: User
<SCRIPT type = "text/JavaScript"> // shift: deletes the first entry of the original array and returns the value of the deleted element. If the array is empty, undefined var arr = [1, 2, 3, 4, 5]; var out = arr. shift (); console. log (ARR); // [2, 3, 4, 5] Console. log (out); // 1 var arr = []; var out = arr. shift (); console. log (ARR); // [] Console. log (out); // undefined // unshift: add the parameter to the beginning of the original array, and return the array length (undefined under IE6) var arr = [1, 2]; vaR out = arr. unshift (-1, 0); console. log (ARR); // [-1, 0, 1, 2] conso Le. log (out); // 4 // POP: Delete the last entry of the original array and return the value of the array to be deleted. If the array is empty, return undefined var arr = [1, 2, 3, 4, 5]; var out = arr. pop (); console. log (ARR); // [1, 2, 3, 4] Console. log (out); // 5 var arr = []; var out = arr. pop (); console. log (ARR); // [] Console. log (out); // undefined // push: add the parameter to the end of the original array, and return the array length var arr = [1, 2, 3]; vaR out = arr. push (4, 5, 6); console. log (ARR); // [1, 2, 3, 4, 5, 6] Console. log (out); // 6 // Concat: return a parameter VaR arr = [1, 2, 3]; var out = arr. concat (4, 5); console. log (ARR); // [1, 2, 3] Console. log (out); // [1, 2, 3, 4, 5] // splice (START, deletecount, val1, val2 ,...): Delete the original array deletecount from the start position, and insert val1, val2 ,..., returns the new array of deleted items var arr = [1, 2, 3, 4, 5]; var out = arr. splice (2, 2, 7, 8, 9, 10); console. log (ARR); // [1, 2, 7, 8, 9, 10, 5] Console. log (out); // [3, 4] // when clearing the array, you only need to pass start. If you do not delete all elements, then pass deletecoun T var arr = [1, 2, 3, 4, 5]; var out = arr. splice (2); console. log (ARR); // [1, 2] Console. log (out); // [3, 4, 5] // same shift var arr = [1, 2, 3, 4, 5]; var out = arr. splice (0, 1); console. log (ARR); // [2, 3, 4, 5] Console. log (out); // [1] // same as unshift var arr = [1, 2, 3, 4, 5]; var out = arr. splice (0, 0, 22,-1, 0); console. log (ARR); // [22,-1, 0, 1, 2, 3, 4, 5] Console. log (out); // [] // same as pop var arr = [1, 2, 3, 4, 5]; VaR out = arr. splice (ARR. length-1, 1); console. log (ARR); // [1, 2, 3, 4] Console. log (out); // [5] // same push var arr = [1, 2, 3, 4, 5]; var out = arr. splice (ARR. length, 0, 6, 7, 8); console. log (ARR); // [1, 2, 3, 4, 5, 6, 7, 8] Console. log (out); // [] // reverse: sorts the original array in reverse order; var arr = ['A', 1, {}, [2, 3]; console. log (ARR); // ['A', 1, {}, [2, 3] var out = arr. reverse (); console. log (ARR); // [[2, 3], {}, 1, 'a'] Console. lo G (out); // [[2, 3], {}, 1, 'a'] var arr = [0, 1, 5, 10, 15]; arr. reverse (); console. log (ARR); // [15, 10, 5, 1, 0] var arr = [1, 0, 5, 15, 10]; arr. reverse (); console. log (ARR); // [10, 15, 5, 0, 1] // sort (orderfunction): Sort () is in ascending order, but the tostring () method of each array item is called first, then compare the strings for sorting, which are compared by ASCII; // different from the reverse principle. When sort () does not contain parameters, it literally sorts the array var arr = [0, 1, 5, 10, 15]; arr. sort (); console. log (ARR); // 0, 1, 10, 15, 5. Note that the character strings are compared by ASCII. // The function sortnumber (, b) {return a-B} arr. sort (sortnumber); console. log (ARR); //, 15 // slice (START, end): return from the original array specified start to end (does not contain this element) if there is only one parameter, VAR arr = [1, 2, 3, 4, 5, 6, 7, 8] From start to the end of the array; vaR out = arr. slice (2, 3); console. log (ARR); // [1, 2, 3, 4, 5, 6, 7, 8] Console. log (out); // [3] // join (separator): combines the elements of the array into a string with the separator, if this parameter is omitted, the default delimiter var arr = [1, 2, 3, 4, 5, 6] is used as the delimiter; var out = arr. join (); console. log (ARR); // [1, 2, 3, 4, 5, 6] Console. log (out); // 1, 2, 3, 4, 5, 6 out = arr. join (""); console. log (out); // 123456 out = arr. join ("&"); console. log (out); // 1 & 2 & 3 & 4 & 5 & 6 </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.