"JS Notes" array of those things [0]

Source: Internet
Author: User

The array in JS is a special object, the index is its property, and the integer index is internally converted to a string type.

1 Creation of arrays

New keyword method:var arr=new Array ()

var arr=New Array (Ten//var arr=New Array  //3

Literal method: var arr=[]

var arr=//0var arr=[1,2,3];

It is recommended to create an array using the literal method.

2 Properties

Length Property Long

3 methods

3.1 Adding a Delete to an element

Push (): Adds an element at the end of the array. The return value is the new array length.

var arr=[1,2,3];arr.push (4//  [1,2,3,4]

Pop (): deletes the element at the end of the array and returns it.

var arr=[1,2,3//[+]

unshift (): adds an element at the beginning of the array. The return value is the new array length.

var arr=[1,2,3];arr.unshift (0//  [0,1,2,3]

Shift (): Deletes the first element of the array and returns it.

var arr=[0,1,2,3//[[+]

Splice ():

Splice (Start,len,i1,i2 ...): Will i1,12 ... insert the position of the Len element from start.

var arr=[0,1,2,3];arr.splice (1,2,0,0); Console.log (arr); //   [0, 0, 0, 3]
var arr=[0,1,2,3];arr.splice (1,2,0//  [0, 0, 3]

Splice (Start,len): Removes Len elements from start.

var arr=[0,1,2,3];arr.splice; Console.log (arr); // [0, 3]

Concat (): Inserts a new element after the array to form a new array.

var arr=[0,1,2,3]; var newarr= Arr.concat,console.log (newarr); // [0, 1, 2, 3, 1, 2]

3.2 Find

IndexOf ():

Finds the parameter element starting at the beginning of the array, returns the index of the parameter element, and returns 1 if the element does not exist.

LastIndexOf ():

Finds from the last element of the array.

3.3 Converting to a string

ToString ()

Join ()

3.4 Sort

Reverse (): reverse order

Sort (): Sorts the strings in alphabetical order.

var arr=[' A ', ten, ' C ', 3];console.log (Arr.sort ()); // [3, "A", "C"]

Sort () can receive a function as the ordering method in the function of the parameter execution.

The function receives two parameters, and if the return value is positive, the order of the two numbers is exchanged in the array, otherwise it is not exchanged. Here are two ways to sort from small to large with sort:

function Compare (A, a    ) {return A-b;} var arr=[1,20,3,10//  [1, 3, ten,]
function Compare (b) {    if (a>b)          return 1;     if (a<b)                   return -1;} var arr=[1,20,3,10//[1, 3, ten,]

"JS Notes" array of those things [0]

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.