Some methods of JS array

Source: Internet
Author: User

1.push ()//can accept any parameter and then add to the end of the array

2.pop ()//stack method, delete a piece of data at the end of the array, and return this data

3.shift ()//queue method, similar to pop (), but instead, deletes a piece of data at the beginning of the array and returns the data.

4.reverse (), sort ()//array sorting method, the sort () method is sorted by default on the first letter of each data, for example: 1,3,34,21,22 If you do not pass in the comparison parameter Array.Sort () is 1,21,22,3,34

In order for it to be sorted numerically, you must add a method, function Campare (val1,val2) {return val2-val1;} and pass this method as a parameter to sort, which can be arranged in descending order Array.Sort (Campare) is 34, 22,21,3,1

5.slice (A, B);//The first parameter is the starting position of the array, and the second parameter is the end position of the array. Method function: From the original array with a as the starting position, b for the end position to take out the data output of the array

6.splice (A,c,[b,d]);//The first parameter start position, the second parameter deletes the number, the third parameter inserts the data

Method action:1> Delete data such as: Splice (A, A, b), delete the data from a as a starting position

2> inserting data such as: Splice (2,0, "Red", "blue") Insert from 2 position Red,blue

3> replace data such as: Splice (2,1, "Red", "blue") delete data from 2 position and insert red blue

Some methods of JS array

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.