JS Array Operation Push,pop,shift,unshift and other methods to introduce _javascript techniques in detail

Source: Internet
Author: User
Tags array length array sort

JS in the array operation of the method is still more, today suddenly thought to sum up, is also known as the temperature of the new bar. But not for each method, I just choose some of them.

Let's start with the push and pop methods, which only push and eject the array from the tail, and operate in the original array, and any changes are an array that affects the operation. Push (args) can press multiple elements at a time and return the updated array length. The pop () function pops up the last element at a time, returns the element that pops up, and returns undefined if the pop () is invoked on the empty group count. If the argument is an array, the entire array is pressed into the original array as an element. does not produce a "split phenomenon" similar to the concat merge array, see the example below

Example 1:
var oldarr=[1,2,3];
Alert (Oldarr.push (4,[5,6])) –>5 (this will only compute [5,6] as an element, returning the updated array length 5)
At this time oldarr–>[1,2,3,4,[5,6]]
Alert (Oldarr.pop ()) –>[5,6] (this pops up the last element [5,6] instead of 6)
At this time oldarr–>[1,2,3,4]
Oldarr.pop () –>4
Oldarr.pop () –>3
Oldarr.pop () –>2
Oldarr.pop () –>1
Oldarr.pop () –>undefined (empty array popup)
Now let's talk about push and pop and see Unshift and shift.
Both methods are operated through the head of an array, others are similar to push and pop, but in IE the Unshift method returns the undefined

Example 2:
var oldarr2=[1,2];
Oldarr2.unshift (3) –>undefined
At this time oldArr2 for –>[3,1,2]
Oldarr2.shift () –>3
At this point oldArr2 is [1,2]
Next, look at the powerful splice, using the elements that can be arbitrarily positioned in the array to add, delete, its operation is also in the original

modified on Array
The start in splice (Start,deletecnt,args) represents the beginning of the subscript, deletecnt represents the number of elements to be deleted from the start subscript (including the element), and the delete operation returns the deleted element. Args represents the replacement of deleted elements (can have multiple parameters), start and deletecnt must be numbers, and if not a number tries to convert, the conversion fails as 0来 processing. Splice must have at least one start element, otherwise no action will be made. DELETECNT does not exist to delete the start and all the following elements (ie, take 0 do not delete). Start can be a negative number, indicating that the calculation starts at the right end of the array. deletecnt if a negative number does not delete, because the negative element cannot be deleted.
Okay, here's the explanation. Now take a look at the example and perhaps a better understanding

Example 3
var oldarr3=[1,2];
Oldarr3.splice () –> "" (Return empty string, no action, after Operation oldarr3–>[1,2])
Oldarr3.splice ("") –>[1,2] ("" attempts to convert to a number failed to return 0, so delete 1, 2, after Operation Oldarr3–>[], but IE is a little disgusting, do not do any operation)
Oldarr3.splice ("1a") –> ditto
Odlarr3.splice (0,2) –>[1,2] ("Delete two elements from the element of subscript 0 1,2 therefore delete oldarr3–>[])
Oldarr3.splice (0,-1) –> "" (Deleted from 0 subscript-1 elements, so equal to no action, after Operation oldarr3–>[1,2])
Oldarr3.splice (1,1) –>2 (delete 1 elements from subscript 1, that is, delete 2, so delete after oldarr3–>[1])
Oldarr3.splice (1,4) –>2 (delete 4 elements from subscript 1, 1 starts with 1 elements, so delete 2, so delete oldarr3–>[1])
Oldarr3.splice ( -1,0,3) –> "" (delete 0 elements from subscript-1 or 2 elements, then add element 3, so after Operation oldarr3–>[1,3,2])
Oldarr3.splice ( -1,1,3) –>2 (1 = 2 element to start delete 1 elements, then add element 3, after operation for oldarr3–>[1,3])
OK then start talking about Concat, which is used to connect two or more arrays that do not change the original array and return only a new array. When a connection is an array, the arguments are connected to elements in a set. Because it's simpler to just start with examples

Example 4:
var oldarr4=[1,2];
Oldarr4.concat (3,4) –>[1,2,3,4]
Oldarr4.concat (3,4,[5,6]) –>[1,2,3,4,5,6] (added here is [5,6] element 5 and Element 6)
Oldarr4.concat (3,[4,[5,6]]) –>[1,2,3,4,[5,6]] (the innermost element here [5,6] is used to add, not disassemble)
Here's how the sorting method in the array sort
Sort (function) is sorted against the original array and does not generate a new array. The default sort () is compared by converting the elements in the array to strings, in the order in which they are sorted in character encoding, and each character has a unique encoding corresponding to the characters.

and look at the example below
var oldarr5=[3,1,5,7,17] Look at this general idea that the Oldarr5.sort () will return to [1,3,5,7,17] as a number from small to large when sorting oldArr5, but look at the results. 1,17,3,5,7] is turned into a string because of comparison. Then make a comparison of the strings one by one if the first character is the same, the second is compared, otherwise the result is returned directly because "17″<" 3″ so that the result of the order is not the result of the general impression.

The sort (function) method can be passed in the custom sorting method in addition to the default, so the result of the sort can be controlled by oneself, how to row on the platoon, is not very cool, hehe. A general custom function comparison that contains two parameters representing the left and right elements to be compared respectively. Then returns a result in a certain way, if the return value is greater than 0 to exchange the left and right elements, if the return value is less than 0 or equal to 0, then the left and right elements are not exchanged. Now take a look at the example

Example 5:
Arrange the original array by number from big to small

Copy Code code as follows:

var oldarr5=[3,1,5,7,17]; Initial array
function Mysort (left,right) {
if (left<right) {
return 1;} Swap two If the left element is less than the right element
else{
return-1;} If the left element is greater than or equal to the right element do not exchange
}

Of course, the above method can be simplified to funaction mysort (left,right) {return right-left;}
Copy Code code as follows:

Sorted by even number in the first odd number
var oldarr6=[3,6,7,18];//initial Array
function MySort2 (left,right) {
if (left%2==0) return-1;//does not exchange if the left element is an even number
if (right%2==0) return 1; Swap if the right element is an even number
return 0; Do not exchange
}

The last slice is only used to intercept some elements in the original array, to return a new array, and the original array will not change, and it operates in a similar way to string slice
Copy Code code as follows:

var oldarr7=[1,2,3,4];
Oldarr7.slice (0) –>[1,2,3,4]
Oldarr7.slice (0,2) –>[1,2]
Oldarr7.slice (0,0) –>[]
Oldarr7.slice (0,-1) –>[1,2,3]
Oldarr7.slice ( -3,-1) –>[2,3]
Oldarr4.slice ( -1,-3) –[]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.