How much does the array method know?

Source: Internet
Author: User

First, what is an array?

Arrays are used to hold multiple data, unlike arrays in other languages, and the array in JS can hold any type of data;

such as: Var ary=[];

var ary=[1,2,3,4,5,6];

var ary=[' A ', ' B ', ' C '];

var ary=[{n:1,m:2},{n:1,m:2},{n:1,m:2}];

var ary=[function a1 () {},function a2 () {}];

Second, how to create an array?

1. Use the array constructor

such as: Var ary=new Array ();

Ary[0]=1;        ary[1]=2; Create an empty array, and then assign the values to each item

var ary=new Array (10); Creates an array and specifies the length of the array

var ary=new Array (1,2,3,4,5); Creates an array and specifies the contents of the array

2, using the array literal notation

var ary=[];

var ary=[1,2,3,4,5,6];

var ary=[' A ', ' B ', ' C ']

var ary=[{n:1,m:2},{n:1,m:2},{n:1,m:2}];

var ary=[function a1 () {},function a2 () {}];

Iii. Basic properties of arrays

Length: Ary.length

Value: Ary[i] The index of the array is starting from 0

Four, there are 12 ways of array:

1. Contact

2. Push

3. Pop

4, Unshift

5, shift

6, Slice

7, Splice

8. Join

9. Sort

10, Reverse

11. toString

12, IndexOf

Five, the characteristics of these methods of the array:

1, concat (); Cloning arrays, Array joins

Parameter: The number of arguments is not fixed, the parameter type is not fixed, when the parameter is not passed to clone a new array

Result: Returns a new array

Does the original array change: does not change

2, Slice (n,m); Gets the region element based on the current array and creates a new array

Parameters: Two parameters, numeric type, representing the index from N, and the index to M (not including m);

Result: Returns the new array that was taken out

Does the original array change: Do not change

3, splice (m,n);  Splice (M,M,X); Insert, replace, delete operations on an array

Delete

Parameters: Two parameters, number type; splice (m,n); Represents the removal of M elements starting at index n

Result: Returns an array of elements to remove

Does the original array change: change

Replace

Parameters: Three parameters, first two numeric types, third indeterminate. Splice (m,n,x); represents the removal of M elements from index n, and adds the newly added X element to this position.

Result: The removed array element is returned

Does the original array change: change

Insert

Parameters: Three parameters, the first two are numeric types, the middle is 0, and the third is indeterminate. Splice (M,0,X); Represents starting at index m, removing 0 elements and adding the newly added X element to this position.

Result: An empty array is returned (no elements have been removed)

Does the original array change: change

4, push (); Adds an element at the end of the specified array

Parameters: variable, variable data type, parameter is newly added element

Result: The last element added

Does the original array change: change

5, Pop (); Deletes the last element of the specified array

Parameters: None

Result: The removed element is returned

Does the original array change: change

6, Unshift (); Add an element before the specified array

Parameters: variable, variable data type, parameter is newly added element

Result: Returns the number of elements in the newly added array (the length of the new array)

Does the original array change: change

7, Shift (); Delete the first element of an array

Parameters: None

Result: The removed element is returned

Does the original array change: change

8. Join (); Add separators to the array

Parameters: one parameter, String type

Result: Returns a string that is strung together with a delimiter

Does the original array change: does not change

9, sort (); Array order from small to large (only valid for numbers within 10)

Arrange for all numbers: Ary.sort (function (ni,n2) {

return n1-n2;

});

Parameters: None

Result: Returns a new array after sorting

Does the original array change: change

10, reverse (); reverse order of the arrays

Parameters: None

Result: Returns a new array after sorting

Does the original array change: change

11, toString (); Convert to String

Parameters: None

Result: Returns the converted string data

Does the original array change: does not change

12, indexOf (); Used to detect whether an element exists in an array, there is an index that returns this element, there is no return-1;

Parameter: The element to be detected; variable parameter, variable data type

This method has compatibility

How much does the array method know?

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.