The JS array method summarizes the addition and deletion of array elements.
Source: Internet
Author: User
The addition and deletion of JS array elements have been confusing. Today, I finally found detailed information and gave me a test Code ^-^ Var arr = new array (); arr [0] = "AAA"; arr [1] = "BBB"; arr [2] = "CCC "; // alert (ARR. length); // 3arr. pop (); // alert (ARR. length); // 2 // alert (ARR [arr. length-1]); // bbbarr. pop (); // alert (ARR [arr. length-1]); // AAA // alert (ARR. length); // 1var arr2 = new array (); // alert (arr2.length); // 0arr2 [0] = "AAA"; arr2 [1] = "BBB "; // alert (arr2.length); // 2arr2. pop (); // alert (arr2.length); // 1arr2 = arr2.slice (0, arr2.length-1); // al ERT (arr2.length); // 0arr2 [0] = "AAA"; arr2 [1] = "BBB"; arr2 [2] = "CCC "; arr2 = arr2.slice (0, 1); alert (arr2.length); // 1 alert (arr2 [0]); // aaaalert (arr2 [1]); // undefinedshift: delete the first entry of the original array and return the value of the deleted element. If the array is empty, return undefined var A = [1, 2, 3, 4, 5]; var B =. shift (); // A: [2, 3, 4, 5] B: 1 unshift: add the parameter to the beginning of the original array, and return the array length var A = [1, 2, 3, 4, 5]; var B =. unshift (-2,-1); // A: [-2,-,] B: 7 Note: In ie6.0, the returned value is always undefined, test return value in ff2.0 Is 7, so the return value of this method is not reliable. You need to use splice instead of this method when returning the value. Pop: Delete the last entry of the original array and return the value of the deleted element. If the array is empty, return undefined var A = [1, 2, 3, 4, 5]; var B =. pop (); // A: [,] B: 5 // you can directly call this operation without returning it: add the parameter to the end of the original array, returns the array length var A = [1, 2, 3, 4, 5]; var B =. push (6, 7); // A: [1, 3, 4, 5, 6, 7] B: 7 Concat: returns a new array, is to add parameters to the original array to form var A = [1, 2, 3, 4, 5]; var B =. concat (6, 7); // A: [1, 3, 4, 5] B: [1, 2, 3, 4, 5, 7] 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 (, 9); // A: [,] B: [] var B =. splice (0, 1); // same as shift. splice (0, 0,-2,-1); var B =. length; // same as unshift var B =. splice (. length-1, 1); // same as pop. splice (. length, 0, 6, 7); var B =. length; // same as pushreverse: returns the reverse order of the array var A = [1, 2, 4, 5]; var B =. reverse (); // A: [5, 4, 3, 2, 1] B: [5, 4, 3, 2, 1] Sort (orderfunction ): sort the array by the specified parameter var A = [1, 2, 3, 4, 5]; var B =. sort (); // A: [1, 2, 3, 4, 5] B: [1, 2, 3, 4, 5] slice (START, end ): returns the array var A = [1, 2, 4, 5]; var B =. slice (); // A: [, 5] B: [, 5] Join (separator): groups the elements of the array into a string, use separator as the separator, otherwise use the default comma as the separator var A = [1, 2, 4, 5]; var B =. join ("|"); // A: [1, 2, 3, 4, 5] B: "1 | 2 | 3 | 4 | 5": Use arrays to simulate javastringbuffer to process strings:/*** string processing function */function stringbuffer () {var arr = new array; this. append = function (STR) {arr [arr. length] = STR ;}; this. tostring = function () {return arr. join (""); // ping the append array into a string };}
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.