There are many methods of arrays, and ECMScript5 provides several methods. Have time to tidy up the notes from the previous cloud, and make it easy for you to find them later.
I. Methods defined in the Array.prototype of Ecmscript 3
1. Join ()
Definition: Converts all elements in an array into strings, and returns the last generated string.
Note: Do not change the original array
var a = [1,2,3];console.log (A.join ()); The default comma-delimited Console.log (A.join ("")) is used. 123 Conversion to String Console.log (A.join ("-"));//1-2-3 Custom delimiter Console.log (a); [Three-in- one] does not modify the primitive group var b = new Array (5); Console.log (B.join (' * '));//**** 4 hyphens make up a string
2, Reverse ()
Definition: Reverses the order of the elements in the array, returning the string in reverse.
Note: The original array is changed
var a2=[1,2,3];console.log (A2.reverse ()); [3,2,1]console.log (A2); [3,2,1] The original array was modified Console.log (A2.reverse (). join ()); Because the original array was modified again.
3. Sort ()
Definition: Sorts the elements in the array, returns the sorted array,
Note: Arrays are sorted in alphabetical order.
The original array is changed
You can customize the sort by non-alphabetic order, as long as a sort () is passed in to a comparative anonymous comparison function.
The comparison function determines the order in which the two parameters are sorted in the array, adding the first parameter to the previous one, and returning a value less than 0, instead. Equal to 0.
var a3=[' C ', ' B ', Undefined, ' a ', ' Z ']; Console.log (A3.sort ()); ["A", "B", "C", "Z", undefined],undefined are discharged to the last Console.log (A3); ["A", "B", "C", "Z", undefined] modify the original array //Custom collation var a4 = [33,4,1111,2222]; Console.log (A4.sort ()); [1111, 2222, 33, 4] default sort a4.sort (function (A, b) {return-a ; }); Console.log (A4); [4, 33, 1111, 2222]
4, Concat ()
Definition: Creates and returns a new array. The new array consists of the parameters in the original array and the concat ()
Note: Do not change the original array
Only one-dimensional arrays can be flattened, high-dimensional (not recursive flattened arrays), high-dimensional arrays (flattened) dimensionality reduction can be seen.
var = [A5]; Console.log (A5.concat (4)); [1,2,3,4] Console.log (A5); [ N/A]//The original array does not change console.log (A5.concat ([4,5])); [1, 2, 3, 4, 5] Console.log (A5.concat ([4,5,[6,7]])); [1, 2, 3, 4, 5,[6,7]]//Only one dimension can be flattened
5, Slice ()
Definition: Returns the fragment or sub-array of the specified array, with two parameters referring to the beginning and end of the fragment (not including the end position).
Note: Do not change the original array
If there are no arguments, it is equivalent to generating a copy of the original array
The resulting new array does not contain an element at the end position
Description: A string can also use this method, such as "[Object Array]". Slice (8,-1); =>array
var a6 = [1,2,3,4,5]; Console.log (A6.slice (1,3)); [2,3] //index from 1 to 3 but including 3 sub-array console.log (A6); [1,2,3,4,5] //The original array does not change console.log (A6.slice (1)); [2,3,4,5] has only one parameter, then from the parameter position to the end Console.log (A6.slice (1,-1)); The [2,3,4] parameter is 1 only to the last element, but does not include it Console.log (A6.slice ( -2,-1)) //[4] from the 2nd count to the penultimate number, but excluding the first number of the countdown
6, Splice ()
Definition: A common method for entering and deleting elements in an array. Returns a new array consisting of deleted elements, returning an empty array without deleting []
The first two parameters are the elements that need to be deleted, and any number of arguments after them are the elements that need to be inserted into the original array
Note: The original array is changed
var a7 = [1,2,3,4,5]; B7 = A7.splice (2); Console.log (b7); [3,4,5] returns the original array from the element indexed to 2 to the end of the deleted part Console.log (A7); the original array is the modified array b7 = A7.splice; B7 = A7.splice (a); Deletes 1 elements from an index of 1 elements Console.log (b7); [2,3] deletes 2 elements from an index of 1, which is 2 and 3 Console.log (A7); [1,4,5] The original array is deleted after the remaining array var b7 = A7.splice (1,0,100); Console.log (b7); [] because no element is deleted, all returned is an empty array Console.log (A7); [1, 100, 2, 3, 4, 5] start with an element with index 1, delete 0 elements, and insert a new element where the original index is 1
7. Push () and pop ()
Definition: Push adds one or more elements to the end of the array, returning the length of the new array. So you can't chain-operate
Pop deletes the last element of the array, returning the value element it deleted
Combine push () and pop () to enable advanced post-out stacks
var a8 = [1,2,3];var B8 = A8.push (4,5); Console.log (B8); 5 Returns the length of the new array. Console.log (A8); [1,2,3,4,5] directly from the original array to add 4 var a9 = [1,2,3];var B9 = A9.pop (); Console.log (B9); 3 The deleted element is returned. Console.log (A9); Delete element 3 directly on the original array
Stack
var stack = [];stack.push (n/a); Console.log (stack); [1,2,3]stack.pop (); Console.log (stack); [1,2]stack.push (Console.log) (stack); [1, 2, 100]stack.pop () console.log (stack); [1, 2]
8, Unshift () and Shift ()
Definition: Unshift is adding one or more elements to the head of an array. If multiple parameters are passed in at once, multiple parameters are added directly to the previous location
Group. Returns the length of the new array for the new array.
Shift: Delete the first element of the array and return the deleted element
Combining push () and () to enable FIFO-first-out queues
var queue = [];queue.push (n/a); Console.log (queue); [1,2,3]queue.shift (); Console.log (queue); [2,3]queue.push (+) Console.log (queue); [2, 3, 100]queue.shift (); Console.log (queue); [3,100]queue.shift (); Console.log (queue); [100]
9. ToString ()
Definition: Converts each element in an array of arrays into a string. The output is a comma-delimited list of strings (possibly the inner elements (arrays) of the array are re-tuned
Using ToString (), the high-dimensional array flattening is the use of this.
Note: The same string that is returned by the Jion () method that is not called with any parameters.
var a10 = [1,2,3];var A11 = [1,2,[3,4,[5,6]]];console.log (a10.tostring ()); 1,2,3console.log (A10); The original array Console.log (a11.tostring ()); 1,2,3,4,6console.log (A11); Original array
The array method in the second, Ecmscript 5
1, ForEach ()//traversal
Definition: The method iterates through an array from beginning to end, invoking the specified function for an element. The transfer function consists of three parameters (array element, element index, array itself
), if you only care about the value of an array element, you can pass only one parameter.
Note: You cannot end prematurely before all elements are passed to the calling function. That is, there is no break statement as in for. If you want to submit a termination, you can
Put the foreach () method in the try block. If the function called by foreach () throws a Foreach.break exception, the loop ends prematurely
var data = [1,2,3];var sum = 0;data.foreach (function (value) { sum+=value;}); Console.log (sum); 6var data = [10,20,30];d Ata.foreach (function (v,i,a) { a[i] = v*2;}); Console.log (data)//[20, 40, 60]
2, Map ()//Map
Definition: Passes each element of the called array to the specified function and returns a new array that contains the return value of the function
Note: The function passed to map should have a return value, return a new array, and not modify the called array.
var data3 = [1,2,3];var b = Data3.map (function (val) { return val*10;}); Console.log (DATA3); [Console.log] does not change the original array (b); [10,20,30] new Array
3, filter ()//filter
Definition: The returned array element is a subset of the called array, the passed function is used to make a logical decision, return True, the element passed to the decision function is the member of this subset.
var data4 = [1,2,3,4,5];var b2 = Data4.filter (function (val) { return val<3;}); Console.log (B2); [1,2]console.log (DATA4); [1,2,3,4,5] does not change the original array var b3 = data4.filter (function (val,i) { return i%2;}); Console.log (b3); [2,4] Returns an array of elements with an odd index
4, every () and some ()
These two methods are logical judgments of arrays, and they are judged by applying the specified function to all elements in the array. Returns TRUE or False
Definition: Every returns true if and only the function that is determined is called for all elements in the array.
Some returns true if at least one is determined to be true.
5. Reduce ()
Definition: Use the specified function to combine array elements to generate a single value. Parameter one is a function that performs a simplified operation, and the second parameter is the initial value passed to the simplified function.
Note: Once confirmed, the traversal of the array is stopped.
var data5 = [1,2,3];var sum = data5.reduce (function (x, y) { return x+y;},0); Console.log (sum); 6var Product = data5.reduce (function (x, y) { return x*y;},100); Console.log (product);
6, IndexOf () and LastIndexOf ()
Definition: Searches for the entire array of seats with the given value, returns the index of the first element found, returns -1;indexof if not found () is the tail from the beginning, lastIndexOf () just opposite
Note: The function is not accepted as a parameter, the first parameter is the value that needs to be searched, the second parameter is optional, which indicates where to search, can be negative, and represents the offset from the end of the array.
Description: The string also has these two methods, which are functionally similar.
var data6 = [1,2,3,3,4,3,5];console.log (Data6.indexof (3)); 2 The index of the first 3 is 2
7, IsArray ()
Definition: Judging whether an object is not an array
Three, string as an array
1, easy to use
var str = "Test"
Str.charat (1)//e
STR[1] //e
2. Methods that can call arrays
Note: strings are immutable values, which are read-only as arrays. So a method such as push () sort () reverse () splice () modifies the original array, which is not valid on the string. Errors may not be prompted!
var str = "Hello"; Array.prototype.join.call (str, ""); "H e l l o" Array.prototype.filter.call (str, function (x) { return X.match (/o/g); ["O"]});
Array Method Daquan