[JavaScript] common APIs for array objects

Source: Internet
Author: User
Concat ()

Concatenate two or more arrays and return results

 
VaRA = [1, 2, 3];VaRB = A. Concat (6, 7); Console. Log ();//[1, 2, 3]Console. Log (B );//[1, 2, 3, 6, 7]
Join (STR)

All elements of the array are separated by STR, which is separated by commas by default.

 
VaRA = [1, 2, 3]VaRB = A. Join ('|'); Console. Log ();//[1, 2, 3]Console. Log (B );//"1 | 2 | 3"
Pop ()

Delete and return the value of the last element of the array

 
VaRA = [1, 2, 3];VaRB =A. Pop (); console. Log ();//[1, 2]Console. Log (B );//3
Push ()

Add one or more elements to the end of the array and return the length of the new array.

 
VaRA = [1, 2, 3];VaRB = A. Push ('4', '5'); Console. Log ();//[1, 2, 3, 4, 5]Console. Log (B );//5
Reverse ()

Reverse the order of elements in the array

VaRA = [1, 2, 3, 4, 5];VaRB =A. Reverse (); console. Log ();//[5, 4, 3, 2, 1]Console. Log (B );//[5, 4, 3, 2, 1]
Shift ()

Delete and return the first element of the array

 
VaRA = [1, 2, 3];VaRB =A. Shift (); console. Log ();//[2, 3]Console. Log (B );//1
Slice (START, end)

Returns the selected element from an existing array.

 VaR A = [1, 2, 3, 4, 5 ];  VaR B = A. Slice (0,-1 );  VaR C = A. Slice (1, 3 );  VaR D = A. Slice (1 );  VaR E = A. Slice (2,-2 );  VaR F = A. Slice (3,1 ); Console. Log ();  //  [1, 2, 3, 4, 5] Console. Log (B );//  [1, 2, 3, 4] Console. Log (C ); //  [2, 3] Console. Log (d ); //  [2, 3, 4, 5] Console. Log (E ); //  [3] Console. Log (f ); //  [] 
Sort (func)

Sorts arrays by specified parameters

Note:Func must return a value.

Negative value. The first parameter is smaller than the second parameter and is placed first.

Zero, equal.

A positive value. The first parameter is later than the second parameter.

  var  A = [000000, ];   var  B = . sort (); console. log (a);  ///   [1, 10, 2, 3]  console. log (B); ///  , 2, 3]   var  num =  function   (x, y) {  return  X- Y ;}  var  C = . sort (Num); console. log (a);  ///   [1, 2, 3, 10]  console. log (c); ///  , 3, 10]  
Splice (START, deletecount, val1, val2 ,···)

Removes one or more elements from an array, inserts a new element at this position, and returns the removed element.

 
VaRA = [1, 2, 3, 4, 5];VaRB = A. splice (2,2, 7,8); Console. Log ();//[1, 2, 7, 8, 5]Console. Log (B );//[3, 4]
Unshift (val1, val2 ,···)

Insert the specified element into the starting position of the array and return the new length.

VaRA = [1, 2, 3];VaRB = A. unshift (-2,-1); Console. Log ();//[-2,-,]Console. Log (B );//5
Tostring ()

Convert the elements of the array into strings, separate them with commas, and connect them.

 
VaRA = [1, 2, 3, 4, 5];VaRB =A. tostring (); console. Log ();//[1, 2, 3, 4, 5]Console. Log (B );//"1, 2, 3, 4, 5"

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.