Array
JavaScript arrays are frequently used in programs, and the methods provided by Array itself are extremely powerful. If you do not have a good grasp of the plug-in, you will not only be stuck or bent in writing programs, but also have obstacles to learning and understanding the source code of other plug-ins. This article mainly lists common functions and new data methods provided by ECMAScript 5.
Arrays in ECMAScript are quite different from arrays in most other languages. Although the ECMAScript array and the arrays in other languages are sorted lists of data, what is different from other languages is that each item of the ECMAScript array can save any type of data, in addition, the major and minor tasks of the ECMAScript array can be dynamically adjusted. It can automatically increase as data is added to accommodate new data.
1. array detection
var isArray = function(obj) {return Object.prototype.toString.call(obj) == '[object Array]';}2. Conversion Method
ToString (), valueOf (), toLocaleString () returns a string that is concatenated in the form of a string of all values in the array.
3. Stack Method
The combination of the push and pop methods can simulate the data structure of the stack's first-in-first-out mode.
The push () method receives any number of parameters, adds them to the end of the array one by one, and returns the length of the modified array.
The pop () method removes the last entry from the end of the array, reduces the length value of the array, and returns the removed entry.
4. Queue Method
The combination of push and shift methods can simulate the data structure of the first-in-first-out queue.
The shift () method removes the first entry from the array and returns the value. At the same time, the length of the array is reduced by 1.
5. Re-sorting method
The reverse () method is used to reverse the order of elements in the array.
The sort () method is used to sort the elements of an array. Note that the sort method compares strings.
Syntax: arrayObject. sort (sortby)
Parameter: sortby is optional. Specify the sorting order. Must be a function.
Returned value: array reference. Note that arrays are sorted on the original array without generating copies.
6. Operation Method
Concat ()Method is used to connect two or more arrays. This method does not change the existing array, but only returns a copy of the connected array.
Syntax: arrayObject. concat (arrayX, arrayX,..., arrayX)
Parameter: required. This parameter can be a specific value or an array object. It can be any number.
Return Value: returns a new array. This array is generated by adding all arrayX parameters to arrayObject. If the concat () operation parameter is an array, the elements in the array are added instead of an array.
Slice ()The method returns the selected element from the existing array.
Syntax: arrayObject. slice (start, end)
Parameters:
Start is required. Specifies where to start selection. If it is a negative number, it specifies the position starting from the end of the array. That is to say,-1 refers to the last element,-2 refers to the second to last element, and so on.
End is optional. Specifies where to end the selection. This parameter is the array subscript at the end of the array fragment. If this parameter is not specified, the split array contains all elements from start to end of the array. If this parameter is a negative number, it specifies the elements starting from the end of the array.
Return Value: return a new array containing elements in arrayObject from start to end (excluding this element.
Note: This method does not modify the array, but returns a subarray. If you want to delete an element from an Array, use the method Array. splice ().
Splice ()This method is used to insert, delete, or replace elements of an array.
Syntax: arrayObject. splice (index, howmany, element1,..., elementX)
Parameters:
Index is required. Specifies where to add/delete elements. This parameter is the subscript of the array element that begins to insert or delete. It must be a number.
Howmany is required. Specifies how many elements should be deleted. It must be a number, but it can be "0 ". If this parameter is not specified, all elements starting from index to the end of the original array are deleted.
Optional. Specifies the new element to be added to the array. Insert from the subscript indicated by index.
ElementX is optional. You can add several elements to the array.
Returned value: if an element is deleted from arrayObject, an array containing the deleted element is returned.
Note: The splice () method can delete zero or multiple elements starting from the index, and replace the deleted elements with one or more values declared in the parameter list.
7. Location Method
The indexOf () and lastIndexOf methods both receive two parameters: the items to be maternity leave and (optional) indicate the index of the start position. The indexOf method looks back from the beginning of the array, and the lastIndexOf method looks forward from the end of the array. Both methods return the position of the item to be searched in the array. If not,-1 is returned.
8. Iteration Method
Five iteration methods defined in ECMAScript 5 are very useful and can be used to process array tasks. The browsers that currently support these methods include IE9 +, Firefox2 +, Safari3 +, Opera9.5 +, and chrome.
Each method receives two parameters: the function to run on each item and (optional) The scope object to run the function-the value that affects this. The function passed in these methods will receive three parameters: the value of the array item, the position of the item in the array, and the array object itself.
Every ()AndSome ()Method, used to query whether the items in the array meet a certain condition. For every (), the input function must return true for each item. This method returns true; otherwise, false. The some () method returns true as long as the input function returns true for one of the values in the array.
var numbers = [1,2,3,4,5,4,3,2,1];var everyResult = number.every(function(item, index, array) {return item > 2;});alert(everyResult);//falsevar someResult = numbers.some(function(item, index, array) {return item > 2;});alert(someResult);//true
Filter ()Method: Use the specified function to determine whether to include a certain item in the returned array, and return this function to return an array consisting of true items. This method is useful for querying all array items that meet certain conditions.
var filterResult = number.filter(function(item, index, array) {return item > 2;});alert(filterResult);//[3,4,5,4,3]
Map ()An array is also returned, and each item of this array is the result of running the input function on the corresponding item in the original array. Returns an array composed of the results of each function call.
var mapResult = numbers.map(function(item, index, array) {return item * 2;});alter(mapResult);//[2,4,6,8,10,8,6,4,2]
ForEach ()The method only runs the input function for each item in the array and does not return a value.
numbers.forEach(function(item, index, array) {//doSomething});