JS Array Learning Notes

Source: Internet
Author: User

Create

1. Create an empty array

var a=[];  // []var a=New//  []

2, create an array and specify the length, the specified length is not the maximum value, the length of the array is longer

var b=New//  [undefinedx3]

3. Create an array and assign a value

var //   [3]var c=New//  ["3"]var//   [3, 4] var d=New//  [3, 4]
Take value
var //   v=4
Modify
d[1]=5;  // array d is [3,5]
New

1. Add a new element (one or more) to the end of the array and return the new length of the array

D.push (6);  // returns 3, array d is [3, 5,6]

2. Add a new element (one or more) to the beginning of the array and return the new length of the array

//   returns 5, array d is [7, 8, 3, 5, 6]

3. Add a new element (one or more) to the specified position of the array, return [], Arrayobject.splice (index,howmany,item1,....., ItemX)

// return [], array d is [7, 8, 1, 2, 3, 5, 6]
Delete

1. Delete the last element and return the element value

D.pop ();    // returns 6, array d is [7, 8, 1, 2, 3, 5]

2. Delete the first element and return the element value

// returns 7, array d is [8, 1, 2, 3, 5]

3. Delete n elements starting at the specified position and return the deleted elements collection

// Delete 2 elements starting with index 2 and return the deleted elements collection [2, 3], array d is [8, 1, 5]
Replace
// first delete the 2 elements starting with index 1, then insert the new element 3,4,5,6 from index 1, return the deleted element collection [1, 5], array d is [8, 3, 4, 5, 6]
Connection

Concat (), used to connect two or more arrays. The method does not alter the existing array, but only returns a copy of the concatenated array.

var a = [];a.concat (4,5); // return Array [1, 2, 3, 4, 5], array A is [1, 2, 3]
var a = [n/a]; var b = [4,5,6];a.concat (b)// return Array [1, 2, 3, 4, 5, 6]
var a = [n/a]; var b = [4,5,6]; var c = [7,8,9];a.concat (b,c)// return Array [1, 2, 3, 4, 5, 6, 7, 8, 9]
array into string

1, join (separator), used to put all the elements in the array into a string.

Separator: Optional. Specifies the delimiter to use. If this argument is omitted, a comma is used as the delimiter.

var a = [];a.join (); // return to "A.join" (""); // return "123"a.join ("-"); "

2, ToString (), can convert the array to a string, and return the result. The return value is the same as the string returned by the join () method without parameters.

reverse Order 

Reverse () that reverses the order of the elements in the array. The method changes the original array without creating a new array.

var a = [];a.reverse (); // return Array [3, 2, 1], array A is [3, 2, 1]
Select

Slice (start,end), returns a new array containing all elements from start to end, excluding the element. The original array is not modified.

Start: Required. Where to start the selection. If it is a negative number, the position is calculated from the end of the array. -1 refers to the last element, 2 refers to the second-lowest element, and so on.

End: Optional. Where to end the selection. If not specified, then the end of the array. If it is a negative number, it starts at the end of the array.

var a = [];a.slice (1); // returns the array [2, 3]a.slice; // returns the array [2]A.slice (-1); // returns the array [3]A.slice ( -2,-1); // return array [2]
Sort

Sort (sortby), which is used to sort elements of an array.

SortBy (A, B): Optional. Specifies the sort order. Must be a function. If not specified, it is sorted in the order in which the characters are encoded.

The return value is greater than 1, then the interchange.

var arr1=[11,1,3,22];arr1.sort (); // return Array [1, one, 3] var arr2=["One", "1", "3", "];arr2.sort"(); // returns the array ["1", "One", "a", "3"] function SortBy (b) {return A- b;  } Arr2.sort (sortby); // returns the array ["1", "3" , "one", "a")
Properties

length, which sets or returns the number of elements in the array.

var e=[1,2,3]; var n=e.length; // n=3e.length=2; // the array e is [1, 2]e.length=5; // array E is [1, 2, undefinedx3]

JS Array Learning Notes

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.