JavaScript array common methods summarizing _javascript tips

Source: Internet
Author: User
Tags array to string javascript array

In this paper, we summarize the common methods of JavaScript array, the details are as follows

1, Join () method:

The Array.join () method converts the elements in the array to string links, returning the last generated string. Optionally, you can specify that the optional string separates the elements of the array in the generated string. If you do not specify a separator, the comma is used by default. The case is as follows:

   var a=[1,2,3];
   A.join ();//=> "1,2,3" because there is no delimiter specified, the default is comma.
   A.join ("+");//=> "1+2+3" specifies that the delimiter is +
   a.join ("-");//=> "1-2-3" to specify the separator as-

The Array.join () method is the inverse of the String.Split () method, which splits the string into chunks to create an array.

2, Reverse () method:

The Array.reverse () method flashbacks the elements in an array and returns an array of flashbacks, which is a flashback in the original array, a new array that does not produce, and returns the original array, except that the elements inside are already in flashbacks. The case is as follows:

   var a=[1,2,3];
   A.reverse ();//=>a=[3,2,1]; 

3, Sort () method:

The Array.Sort () method sorts the elements in the array and returns the sorted array. Sort in alphabetical order by default without passing parameters. The case is as follows:

   var a=[4,3,1,2]
   a.sort ();//=>[1,2,3,4]
   a.sort (function (a,b) {return a-b;}); /=>[1,2,3,4] Descending order
   a.sort (function (a,b) {return b-a;}); /=>[4,3,2,1] Ascending order

It is convenient to use anonymous functions here, because a function is not necessary to name a function once it is used once.

4, Concat ():

The Array.concat () method creates and returns a new array in which the elements in the new array contain the elements that call the array and the values passed in in Concat (), which can be either a separate value or an array, and concat () does not recursively flatten an array of arrays. The case is as follows:

  var a=[5,6,7];
  A.concat (1,2);//=>[5,6,7,1,2];
  A.concat ([1,2]);//=>[5,6,7,1,2];
  A.concat (3,[1,2]);//=>[5,6,7,3,1,2];
  A.concat ([1,[2,3]]);//=>[5,6,7,1,[2,3]]; 

5, Slice () method:

The Array.slice () method returns a fragment or a child array of the specified array. It can be passed a parameter or two parameters, the parameters can be positive or negative. The case is as follows:

 var a=[5,6,7,3,1,2];
 A.slice (1)//=>[6,7,3,1,2] Numeric parameter refers to the index of an array, a parameter represents the starting position, and the second argument defaults to the number of elements in the group.
  A.slice (1,3)//=>[6,7] The second parameter is the end position of the array index, (not included) index>=1&&index<3;
 A.slice (1,-3)//=>[6,7] when there is a negative number of parameters, you can convert positive numbers, the method is -3+6 (the number of elements in the array)
  A.slice ( -3,-2)//=>[3] the same as above. 

6, Splice () method:

The Array.splice () method is a common method for inserting or deleting in an array. It modifies the called Array, splice () can pass in three parameters, the first parameter indicates where the index of the element is deleted, the second parameter represents the total number of deleted elements, the third parameter represents the inserted element, and the position where the element is inserted is where the element begins. The case is as follows:

  var a=[5,6,7,3,1,2];
 A.splice (2);//=>[7,3,1,2] a=[5,6];//pass in a parameter representing all elements after the start of the index deletion.
 A.splice (2,2);//=>[7,3]   a=[5,6,1,2]; The second parameter represents the number of deleted elements.
 A.splice (2,2, ' A ', ' B ', ' C ');//=>[7,3] a=[5,6, ' A ', ' B ', ' C ', 1,2]; 

7, push () and Unshift () method:

The Array.push () method is to add an element to the end of the array, which returns the length of the new array; the Array.unshift () method is to add the element to the front of the array, returning the length of the new array. The case is as follows:

 var a=[1,2,3];
 A.push (4,5);//a=[1,2,3,4,5]; The return value is 5;
 a.unshift (4,5);//a=[4,5,1,2,3]; parameters that return a value of 5;☆ can be one or multiple.

8, Pop () and Shift () method:

The Array.pop () method is to delete the last element in the array, which returns the deleted element; the Array.shift () method deletes the first element of the array and returns the element that was deleted.

var a=[5,6,7];  
A.pop ();//a=[5,6]; The return value is 7  
a.shift ();//a=[6,7]; return value 5

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.