vararr =NewArray (); 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);//1varARR2 =NewArray ();//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);//1Alert (arr2[0]);//AAAAlert (arr2[1]);//undefinedShift: Deletes the first item of the original array and returns the value of the deleted element; returns undefined if the array is emptyvarA = [1,2,3,4,5]; varb = A.shift ();//a:[2,3,4,5] B:1unshift: Adds a parameter to the beginning of the original array and returns the length of the arrayvarA = [1,2,3,4,5]; varb = A.unshift ( -2,-1);//a:[-2,-1,1,2,3,4,5] B:7Note: The Test return value under IE6.0 is always undefined,ff2.0 Test return value is 7, so the return value of this method is not reliable, need to use the return value when the splice can be used instead of this method. Pop: Deletes the last item of the original array and returns the value of the deleted element; returns undefined if the array is emptyvarA = [1,2,3,4,5]; varb = A.pop ();//a:[1,2,3,4] B:5//Do not return the words directly to call on it.push: Adds a parameter to the end of the original array and returns the length of the arrayvarA = [1,2,3,4,5]; varb = A.push (6,7);//a:[1,2,3,4,5,6,7] B:7concat: Returns a new array that consists of adding parameters to the original arrayvarA = [1,2,3,4,5]; varb = A.concat (6,7);//a:[1,2,3,4,5] b:[1,2,3,4,5,6,7]Splice (Start,deletecount,val1,val2,...) : Deletes the DeleteCount entry starting from the start position and inserts the Val1,val2 from that position,...varA = [1,2,3,4,5]; varb = A.splice (2,2,7,8,9);//a:[1,2,7,8,9,5] b:[3,4]varb = A.splice (0,1);//with ShiftA.splice (0,0,-2,-1);varb = a.length;//with Unshiftvarb = A.splice (a.length-1,1);//with popA.splice (a.length,0,6,7);varb = a.length;//with PushReverse: Reverse the arrayvarA = [1,2,3,4,5]; varb = A.reverse ();//a:[5,4,3,2,1] b:[5,4,3,2,1]sort (orderfunction): Sorts the array by the specified parametersvarA = [1,2,3,4,5]; varb = A.sort ();//a:[1,2,3,4,5] b:[1,2,3,4,5]Slice (start,end): Returns a new array that consists of the entries from the original array that specify the starting subscript to the end subscriptvarA = [1,2,3,4,5]; varb = A.slice (2,5);//a:[1,2,3,4,5] b:[3,4,5]Join (Separator): Sets the element group of the array as a string, separator as a delimiter, or by default with a comma delimiter if omittedvarA = [1,2,3,4,5]; varb = A.join ("|");//a:[1,2,3,4,5] B: "1|2|3|4|5"another way to manipulate strings using the array emulation Javastringbuffer:/** * String handler function*/functionStringBuffer () {vararr =NewArray; This. Append =function(str) {arr[arr.length]=str;}; This. toString =function(){ returnArr.join ("");//Ping the Append incoming array into a string};} In the application today, it is suddenly found that join is a good way to convert an array into a string, so it is encapsulated into objects using:/** * Convert an array into a string of specific symbol splits*/functionarraytostring (arr,separator) {if(!separator) separator = "";//separator is null, default is null returnArr.join (separator);}/** * Find the string contained in the array*/functionarrayfindstring (arr,string) {varstr = Arr.join (""); returnStr.indexof (string);}
JavaScript array manipulation