A method of operating an array summary JavaScript

Array (Array) JavaScript is the most commonly used type of. The ECMAScript array is an ordered list of data. The array can store any type of data. Size of the array can be dynamically adjusted, either automatically grow with the addition of data to accommodate new data.

ECMAScript array provides several methods for handling and processing of the array, the following method of operation of the array of finishing their own.

1.push (): add elements to the end of the array, and returns the modified array length. (You can accept any parameters)

 

 push () method accepted parameter may be any type, may also be an array of objects and the like .

 Twice or more using the push () method, if push () method takes a parameter of type Array, then the last one is the original operation of an array, as shown above, if each element of an array would like to add to among the array of operations, we need to expand the array to be added,

2.pop: Remove Items at the end of the array, and returns the item removed.

 As can be seen in FIG, pop returns the contents of the removed rear () method returns an array of the original operation, pop () method does not accept any parameters.

3.shift (): removing the leading end of the first array and the return, while the length of the array -1, and will not accept any parameters .

 4.unshift (): add any entry in the array and return the distal length of the array. Parameter is accepted operation content to the array is added if necessary, any type of acceptable parameters.

 5.Reverse (): Array will reverse the original order.

 6.sort (): sort in ascending order Method default array, i.e., the smallest one at the top, the largest at the bottom surface.

Because sort method is to compare a string, so it can accept a comparison function as an argument to specify a position in front of which is located in which position.
Comparison function accepts two parameters, the first parameter if the second should be located before a negative number is returned, if the parameters are equal Returns 0 if the first parameter a should be located after the second integer is returned.

Accordance with the rules can write the following comparison function:

function compare (value1,value2){
if((value1<value2){
return -1
}else if((value1>value2){
return 1
}else {
return 0
}
}

 From ordering large value to a small value can be written as:

  

 Or may be written by a simple method:

function compare (value1,value2) {
value1- value2
}
or
function compare (value1,value2) {
value2- value1
}

 7.contact (): This method may create a new array of options based on all current array. (It can be said to be connected to two arrays).

 8.slice (): It can be a new array based on the current array of one or more items created.

The Slice () method can accept one or two parameters, i.e., return to the starting position and ending position of the item, in a case where only one parameter, slice method returns all entries from the parameter specifies the current position to the end of the array. If there are two parameters, the method returns item between the start and end positions - but not including the end position of the item. Slice method does not affect the original array.

 9.splice: This method is probably the most powerful array of methods. It has a variety of uses, the main usage is inserted into the middle of an array of items, but the way of using this method, there are about three kinds.

(1) Delete:  You can delete any number of items, only you need to specify two parameters: the number of items to be deleted and the position of the first item to be deleted. Such as splice (0,2) deletes array operated by the first two. And return the contents of the deleted.

 

 (2) is inserted: can insert any number of entries to the specified position, only need to provide three parameters: starting position 0 (the number of items to be removed) and the items to be inserted.

For example: splice (2,0, "nihao" "hello") 2 will be inserted starting from the two parameters array location.

 (3) alternatively: any number of items can be inserted to the specified position, and delete any number of entries.

Simply specify three parameters: the number of items to be inserted and any number of the start position, the entry to be deleted, for example, splice (2,1, "green" "red") will delete the item 2 of the current array position, and then from the position 2 begin inserting the string "red" "green". After that is inserted into the first deleted.

10. indexOf (): begin at the end of the process from the beginning of the array start looking backwards, lastIndexOf () method from the array of looking forward.

Both methods have returned to find the location of items in the array, or -1 in the case of not found, the process in operation, the operator will use the congruent; that is, to find the required item must strictly equal.

 

 

 

 

 

 

 11. The  Every (): given function, if the function returns true for each item, return true in the array each run.

 

 

 12. The array of the array for each run a given function, the function returns true returns of items consisting of: filter ().

 

 

13.forEach (): to run on each item in the array given function. This method has no return value. Is essentially the same as in the for loop

14.map (): to run on each item in the array for a given function that returns the results of each array of function call.

 

Guess you like

Origin www.cnblogs.com/taxpolat/p/11778353.html