JS Array method

Source: Internet
Author: User

Array method
Empty array
1:arr.length=0
2:arr=[]
Arr.push ()//to the last element of the array, a return value will be added, which is the new array length
Arr.unshift ()//To add an element to the first array, and also return a new array length
IE6 7 does not support Unshift return value
Arr.pop ()//delete the last element, return the deleted element
Arr.shift ()//delete the previous element, return the deleted element
arr.splice//Delete, replace, add
Arr.splice (0,1)//delete one from the beginning of 0
Arr.splice (0,2,3)//with a third substitution starting from zero two
Arr.splice (1,0,3)//
Add 3 to the 1 position

Array de-weight? bugs
for (var i=0; i<arr.length; i++) {
for (Var j=i+1;j<arr.length-1;j++) {
if (Arr[i]==arr[j]) {
Arr.splice (j,1)
j--
}
}
}

Array sorting
Arr.sort ()//Arrange By string
Arr.sort (function (b) {
Return a-b;//from small to large
Return b-a;//from large to small
})
Random sort
Arr.sort (function (b) {
Return Math.random ()-0.5
})

Mathematical Methods
Math.Round ()//rounding
Math.random ()//0~1 random number
Math.ceil ()//Upward value
Math.floor ()//down-fetch value

Arr1.concat (ARR2,ARR3)
Stitch the array together
Arr.reverse ()
Reverse array Order

Array Iteration Method

Arr.foreach (function (e) {Console.log (1)})

JS Array method

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.