The usual array methods in JavaScript are explained in detail

Source: Internet
Author: User

Summary of common methods for arrays in JavaScript 1, Join () method: The Array.join () method converts the elements in the array to strings, and returns the last generated string. Optionally, you can specify an optional string to separate the elements of the array in the resulting string. If you do not specify a delimiter, commas are used by default. The cases are as follows:
     var  a=[1,2,3];      a.join();//=> "1,2,3" 因为没有指定分隔符,默认为逗号。      a.join("+");//=> "1+2+3" 指定分隔符为+      a.join("-");//=> "1-2-3" 指定分隔符为-      Array.join()方法是String.split()方法的逆操作,后者是将字符串分割成若干块创建一个数组。
2, Reverse () method: Array.reverse () method will be in the array of elements in a flashback, return to the array of flashbacks, it is in the original array of flashbacks, not produce a new array, return is the original array, but the elements have been in the flashback re-reflow. The cases are 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. If you do not pass parameters, they are sorted alphabetically by default. The cases are 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] 降序排列     a.sort(function(a,b){return b-a;});//=>[4,3,2,1] 升序排列      这里使用匿名函数很方便,因为函数只使用一次就没有必要给函数命名了。
4, Concat (): The Array.concat () method creates and returns a new array in which the elements in the new array contain the values of the arguments passed in the elements of the call array and concat (), which can be either individual values or arrays, concat () Arrays that do not recursively flatten arrays. The cases are 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];   
5, Slice () method: The Array.slice () method returns a fragment or sub-array of the specified array. Inside can pass a parameter or two parameter, the parameter can be positive also can be negative. The cases are as follows:
 var a=[5,6,7,3,1,2];  a.slice(1)// =>[6,7,3,1,2]  数字参数指的是数组的索引,一个参数表示开始的位置,不传第二个参数默认为数组中元素个数。   a.slice(1,3)// =>[6,7]  第二参数是数组索引结束位置,(不包含) index>=1&&index<3;  a.slice(1,-3)// =>[6,7]  当参数中有负数时候,可以进行转换正数,方法就是-3+6(数组里元素个数)   
6. Splice () Method: The Array.splice () method is a generic 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 deleted element begins, the second parameter represents the total number of deleted elements, the third parameter represents the inserted element, and the position where the element was inserted is where the element was deleted. The cases are as follows:
  var a=[5,6,7,3,1,2];  a.splice(2);// =>[7,3,1,2]  a=[5,6];//传入一个参数表示从索引开始删除之后所有的元素。  a.splice(2,2);// =>[7,3]     a=[5,6,1,2];第二个参数表示删除元素的个数。  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 last face of the array, which returns the length of the new array, and the Array.unshift () method is to add the element to the front of the array, returning the length of the new array. The cases are as follows:
 var  a=[1,2,3];  a.push(4,5);// a=[1,2,3,4,5]; 返回 值为5;  a.unshift(4,5);// a=[4,5,1,2,3]; 返回 值为5;☆传的参数可以一个,也可以多个。
8, Pop () and Shift () method: The Array.pop () method is to delete the last element in the array, it returns the element that is deleted, and the Array.shift () method is to delete the first element of the array and return the element that is deleted.
   var a=[5,6,7];      a.pop();// a=[5,6]; 返回值为 7       a.shift();// a=[6,7];  返回值 5

The usual array methods in JavaScript are explained in detail

Related Article

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.