There are two ways to create arrays in array creation JavaScript, the first of which is to use the array constructor:varARR1 =NewArray ();//create an empty arrayvarARR2 =NewArray (20);//create an array with 20 itemsvarARR3 =NewArray ("Lily", "Lucy", "Tom");//create an array with 3 stringsthe second basic way to create an array is by using the array literal notation:varARR4 = [];//create an empty arrayvarARR5 = [20];//create an array with 1 itemsvarARR6 = ["Lily", "Lucy", "Tom"];//create an array with 3 stringswhen reading and setting the value of an array, use square brackets and provide the corresponding values based on the0the numeric index:varARR6 = ["Lily", "Lucy", "Tom"];//create an array with 3 stringsAlert (arr6[0]);//LilyARR6[1] = "Mary";//Modify the second item to MaryARR6[3] = "Sean";//add fourth to SeanThe length property of an array in JavaScript can be modified, as shown in the following example:vararr = ["Lily", "Lucy", "Tom"];//create an array with 3 stringsArr[arr.length] = "Sean";//add an "Sean" at Subscript 3 (that is, the tail of the array)Arr.length = arr.length-1;//Delete the last item of the arrayIf you need to determine if an object is not an array object, before ECMAScript 5, we caninstanceofarray to judge, but the problem with the instanceof operator is that it assumes that there is only one global execution environment.
If a page contains more than one frame, there are actually more than two different global execution environments, and there are more than two different versions of the Array constructor. If you pass an array from one frame to another, the incoming array has a different constructor than the array that was created natively in the second frame. ECMAScript5added the Array.isarray () method. The purpose of this method is to ultimately determine whether a value is an array, regardless of which global execution environment it was created in. The array method below begins to introduce the array method, the array method has the array prototype method, but also has the method which inherits from the object objects, here we only introduce the array prototype method, the array prototype method mainly has the following: join () push () and pop () shift () and Unshift ( ) sort () reverse () concat () Slice () splice () indexOf () and LastIndexOf () (ES5 New) ForEach () (ES5 New) map () (ES5 Add) filter () (ES5 New) every () (ES5 New) Some () (ES5 new) reduce () and Reduceright () (ES5 New) New method browser support for ES5: Opera11+Firefox3.6+Safari5+Chrome8+Internet Explorer+for supported browser versions, this can be done through the array prototype extension. The basic functions of each method are described in detail below. 1, join () Join (separator): Sets the element group of an array as a string, separator as a delimiter, or by default with a comma delimiter, which receives only one argument: the delimiter. vararr = [A.];console.log (Arr.join ()); // theConsole.log (Arr.join ("-"));// theConsole.log (arr);//[1, 2, 3] (the original array is unchanged)by using the Join () method, you can implement a repeating string, simply passing in the string and repeating the number of times, to return the duplicate string, the function is as follows:functionrepeatstring (str, n) {return NewArray (n + 1). Join (str); Console.log (Repeatstring ("ABC", 3));//ABCABCABCConsole.log (repeatstring ("Hi", 5));//Hihihihihi2, push (), and Pop () push (): You can receive any number of arguments, add them to the end of the array one by one, and return the length of the modified array. Pop (): Removes the last item at the end of the array, reduces the length value of the array, and then returns the item that was removed. vararr = ["Lily", "Lucy", "Tom"];varCount = Arr.push ("Jack", "Sean"); Console.log (count); //5Console.log (arr);//["Lily", "Lucy", "Tom", "Jack", "Sean")varitem =Arr.pop (); Console.log (item);//SeanConsole.log (arr);//["Lily", "Lucy", "Tom", "Jack")3, Shift (), and Unshift () Shift (): Deletes the first item of the original array and returns the value of the deleted element, or undefined if the array is empty. Unshift: Adds a parameter to the beginning of the original array and returns the length of the array. This set of methods corresponds to the above push () and Pop () methods, one is the beginning of the operation array, and the other is the end of the operand array. vararr = ["Lily", "Lucy", "Tom"];varCount = Arr.unshift ("Jack", "Sean"); Console.log (count); //5Console.log (arr);//["Jack", "Sean", "Lily", "Lucy", "Tom")varitem =Arr.shift (); Console.log (item);//JackConsole.log (arr);//["Sean", "Lily", "Lucy", "Tom")4, sort () sort (): Arranges array items in ascending order-that is, the smallest value is at the front and the largest value is the last. When sorting, the sort () method invokes the ToString () transformation method for each array item, and then compares the resulting string to determine how to sort. The sort () method compares strings, even if each item in the array is a numeric value, so the following occurs:varARR1 = ["A", "D", "C", "B"];console.log (Arr1.sort ()); //["A", "B", "C", "D"]ARR2 = [13, 24, 51, 3];console.log (Arr2.sort ()); //[3 ,--]Console.log (ARR2);//[13, 24, 3, 51] (the meta array is changed)To solve the above problem, the sort () method can receive a comparison function as a parameter so that we specify which value is in front of which value.
The comparison function receives two parameters, returns a negative number if the first argument should precede the second, and returns if two arguments are equal0returns a positive number if the first parameter should be located after the second one. Here is a simple comparison function:functionCompare (value1, value2) {if(Value1 <value2) {return-1;} Else if(Value1 >value2) {return1;} Else {return0;}} ARR2= [13, 24, 51, 3];console.log (Arr2.sort (Compare)); //[3 ,--]If you need to produce a descending sort result by comparing functions, simply swap the values returned by the comparison function:functionCompare (value1, value2) {if(Value1 <value2) {return1;} Else if(Value1 >value2) {return-1;} Else {return0;}} ARR2= [13, 24, 51, 3];console.log (Arr2.sort (Compare)); //[Wuyi, 3]5, reverse () reverse (): Reverses the order of the array items. vararr = [13, 24, 51, 3];console.log (Arr.reverse ()); //[3, Wuyi, a.]Console.log (arr);//[3, 51, 24, 13] (original array changed)6, Concat () concat (): Adds a parameter to the original array. This method creates a copy of the current array, adds the received parameters to the end of the copy, and finally returns the newly constructed array. Without passing arguments to the concat () method, it simply copies the current array and returns a copy. vararr = [1,3,5,7];varArrcopy = Arr.concat (9,[11,13]); Console.log (arrcopy); //[1, 3, 5, 7, 9, one, +]Console.log (arr);//[1, 3, 5, 7] (the original array has not been modified)from the above test results can be found: the incoming is not an array, the parameter is added directly after the array, if the array is passed in, the array of the individual items are added to the array. But what if a two-dimensional array is passed in?varArrCopy2 = Arr.concat ([9,[11,13]]); Console.log (ARRCOPY2); //[1, 3, 5, 7, 9, array[2]]Console.log (Arrcopy2[5]);//[one by one]in the code above, the fifth item of the ARRCOPY2 array is an array of two items, that is, the Concat method can only add each item in the array to the arrays, and if some of the entries in the array are arrays, the array entry is also added to the ArrCopy2 as an item. 7, Slice () slice (): Returns a new array that consists of the entries from the original array that specify the starting subscript to the end subscript. The slice () method can accept one or two parameters, that is, to return the starting and ending positions of an item.
In the case of only one argument, the slice () method returns all items starting at the specified position of the parameter to the end of the current array. If there are two parameters, the method returns the item between the start and end positions--but not the end position. vararr = [1,3,5,7,9,11];varArrcopy = Arr.slice (1);varArrCopy2 = Arr.slice (1,4);varArrCopy3 = Arr.slice (1,-2);varArrCopy4 = Arr.slice ( -4,-1); Console.log (arr); //[1, 3, 5, 7, 9, 11] (the original array does not change)Console.log (arrcopy);//[3, 5, 7, 9, each]Console.log (ARRCOPY2);//[3, 5, 7]Console.log (ARRCOPY3);//[3, 5, 7]Console.log (ARRCOPY4);//[5, 7, 9]arrcopy only set a parameter, that is, the starting subscript is 1, so the returned array is subscript 1 (including subscript 1) to start to the end of the array. The arrCopy2 sets two parameters, returning a sub-array starting with the subscript (including 1) to the terminating subscript (excluding 4). ArrCopy3 set two parameters, terminating subscript is negative, when negative, negative number plus the value of the array length (6) to replace the number of the position, so it is a sub-array starting from 1 to 4 (not included). Both parameters in ArrCopy4 are negative, so the array length of 6 is converted to positive numbers, so it is equivalent to slice (2,5). 8, Splice () splice (): Very powerful array method, it has many usages, can implement delete, insert and replace. Delete: You can delete any number of items, just specify2 parameters: The position of the first item to delete and the number of items to delete. For example, splice (0,2) Deletes the first two items in the array. Insert: You can insert any number of items to the specified location, simply provide3 parameters: Start position, 0 (number of items to delete), and item to insert. For example, splice (2,0,4,6) from the current array position 2start inserting 4 and 6. Replace: You can insert any number of items to the specified location, and delete any number of items at the same time, just specify3 parameters: The starting position, the number of items to delete, and any number of items to insert. The number of items inserted does not have to be equal to the number of items deleted. For example, splice (2,1,4,6) deletes the entry for the current array position 2, and then from position 2start inserting 4 and 6. The splice () method always returns an array containing the items removed from the original array, and an empty array if no items are deleted. vararr = [1,3,5,7,9,11];vararrremoved = Arr.splice (0,2); Console.log (arr); //[5, 7, 9, one]Console.log (arrremoved);//[1, 3]varArrRemoved2 = Arr.splice (2,0,4,6); Console.log (arr); //[5, 7, 4, 6, 9, one]Console.log (ARRREMOVED2);// []varArrRemoved3 = Arr.splice (1,1,2,4); Console.log (arr); //[5, 2, 4, 4, 6, 9, one]Console.log (ARRREMOVED3);//[7]9, IndexOf (), and LastIndexOf () indexOf (): Receives two parameters: the subparagraphs to find (optional) represents the index at which to find the starting point. Where, from the beginning of the array (position0) to start looking backwards. LastIndexOf: Receives two parameters: the subparagraphs (optional) to find indicates the index at which to find the starting point. Where you start looking forward from the end of the array. Both of these methods return the position of the item you are looking for in the array, or return if it is not found?1. The congruent operator is used when comparing the first argument to each item in the array. vararr = [1,3,5,7,7,5,3,1];console.log (Arr.indexof (5));//2Console.log (Arr.lastindexof (5));//5Console.log (Arr.indexof (5,2));//2Console.log (Arr.lastindexof (5,4));//2Console.log (Arr.indexof ("5"));//-110, foreach () foreach (): Iterates through the array and runs the given function for each item in the array. This method has no return value. Parameters are function types, the default is the parameter, the parameters are: traversed array contents, the corresponding array index, the array itself. vararr = [1, 2, 3, 4, 5];arr.foreach (function(x, Index, a) {Console.log (x+ ' | ' + index + ' | ' + (a = = =arr);});//the output is://1|0|true//2|1|true//3|2|true//4|3|true//5|4|true11, Map () map (): Refers to a "map" that runs the given function for each item in the array, and returns a list of the results of each function call. The following code uses the map method to achieve the square of each number in the array. vararr = [1, 2, 3, 4, 5];varARR2 = Arr.map (function(item) {returnitem*item;}); Console.log (ARR2); //[1, 4, 9, +,]12, filter () filter (): "Filter" function, each item in the array runs the given function, returning an array that satisfies the filter condition. vararr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];varARR2 = Arr.filter (function(x, index) {returnIndex% 3 = = = 0 | | X >= 8;}); Console.log (ARR2); //[1, 4, 7, 8, 9, ten]13, every () every (): Determines whether each item in the array satisfies the condition, and returns true only if all items satisfy the condition. vararr = [1, 2, 3, 4, 5];varARR2 = Arr.every (function(x) {returnX < 10;}); Console.log (ARR2); //truevarARR3 = Arr.every (function(x) {returnX < 3;}); Console.log (ARR3); //false14, some () some (): Determines if there are any items in the array that satisfy the condition, and returns true if one satisfies the condition. vararr = [1, 2, 3, 4, 5];varARR2 = Arr.some (function(x) {returnX < 3;}); Console.log (ARR2); //truevarARR3 = Arr.some (function(x) {returnX < 1;}); Console.log (ARR3); //false15, reduce (), and Reduceright () both implement all the items of the iterated group, and then build a value that will eventually be returned. The reduce () method starts from the first item in the array and traverses through to the end. The Reduceright () begins with the last item in the array and traverses forward to the first item. Both methods receive two parameters: a function that is called on each item and (optionally) the initial value that is the base of the merge. function received to reduce () and reduceright ()4parameters: The previous value, the current value, the index of the item, and the array object. Any value returned by this function will be automatically passed to the next item as the first argument. The first iteration occurs on the second item of the array, so the first parameter is the first item of the array, and the second is the second item of the array. The following code uses reduce () to implement an array summation, and the array begins with an initial value of 10. varvalues = [1,2,3,4,5];varsum = Values.reduceright (function(prev, cur, index, array) {returnPrev +cur;},10); Console.log (sum); // -
Processing of an array of JavaScript (i)