This article mainly introduces the operation method of Array in JS and explains it with code examples. If you need it, you can refer to the addition and deletion of Array elements in js, today, I finally found the detailed information and gave me the code for testing ^-^
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); // alert (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 undefinedvar a = [1, 2, 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, in FF2.0, the test return value is 7, so The return value is unreliable. 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, undefinedvar a = [1, 2, 3, 4, 5] is returned. 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 ,... when clearing the array, you only need Pass startIndex. If you do not delete all elements, pass the deleteCount parameter. Splice also has the ability to delete and then add, that is, delete several elements first, and then add several elements at the location of the deletion. The number of deleted elements is not equal to the number of added elements, deleteCount is also used at this time. Var a = [1, 2, 3, 4, 5]; var B =. splice (, 9); // a: [,] B: [] var B =. splice (0, 1); // same as shifta. splice (0, 0,-2,-1); var B =. length; // same as unshiftvar B =. splice (. length-1, 1); // same as popa. 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. leng Th] = str ;}; this. toString = function () {return arr. join (""); // ping the append array into a string};} today, in the application, we suddenly found that join is a good way to convert the array into a string, therefore, it is encapsulated into an object./*** is used to convert the array into a string separated by a specific symbol */function arrayToString (arr, separator) {if (! Separator) separator = ""; // If separator is null, return arr is empty by default. join (separator);}/*** find the string contained in the array */function arrayFindString (arr, string) {var str = arr. join (""); return str. indexOf (string );}
For more information about how to operate arrays in JS, see PHP!