JS Notes: Array method collation

Source: Internet
Author: User

Add 1.push () syntax: Arrayobject.push (newele1,newele2,..., Newelex); function: Adds a parameter in push to the trailing return value of Arrayobject: Adds the specified value to the new length after the array   Example: var arr = [8,7,5,2,8,4,3];var Arrlen = Arr.push ("U", "R", "G"); Console.log (Arrlen); 10console.log (arr); [8, 7, 5, 2, 8, 4, 3, "U", "R", "G"] 2.unshift () Syntax: Arrayobject.unshift (newele1,newele2,..., Newelex); Function: Adds a parameter in unshift to the trailing return value of Arrayobject: Adds the specified value to the array after the new length   example: var arr = [8,7,5,2,8,4,3];var Arrlen = Arr.unshift ("U", "R "," G "); Console.log (Arrlen); 10console.log (arr); ["U", "R", "G", 8, 7, 5, 2, 8, 4, 3]  II, delete 1.pop () syntax: Arrayobject.pop () function: Delete the last element of the Arrayobject return value: Deleted elements   Example: var arr = [8,7,5,2,8,4,3];var Arrele = Arr.pop (); Console.log (Arrele); 3console.log (arr); [8, 7, 5, 2, 8, 4] 2.shift () syntax: Arrayobject.shift () function: Delete the first element of the Arrayobject return value: Deleted element   Example: var arr = [ 8,7,5,2,8,4,3];var Arrele = Arr.shift (); Console.log (Arrele); 8console.log (arr); [7, 5, 2, 8, 4, 3]  three, delimited array 1.join () syntax: arrayobject.join (separator) function: Separates an array with a separator (separator) and puts a string return value: String &nbspExample: var arr = [8,7,5,2,8,4,3];var newstr = Arr.join ("-"); Console.log (NEWSTR); 8-7-5-2-8-4-3console.log (arr); [8, 7, 5, 2, 8, 4, 3]  four, flip array 1.reserve () syntax: Arrayobject.reserve () function: Reverses the order of the elements in the array return value: Array   Example: var arr = [ 8,7,5,2,8,4,3];var NEWARR = Arr.reverse (); Console.log (NEWARR); [3, 4, 8, 2, 5, 7, 8]console.log (arr); [3, 4, 8, 2, 5, 7, 8]  Five, array sort 1.sort () syntax: Arrayobject.sort () function: Sorts the elements of an array, and every item in a timely manner is a numeric value, and the sort () method compares the string; ) can receive a comparison function as a parameter in ascending--arr.sort (function (A, b) {return-A-a}) return value in descending--arr.sort (function (A, b) {return b-a}): Array   example var arr = [8,7,5,2,8,4,3];var NEWARR = Arr.sort (function (A, b) {return b-a}); Console.log (NEWARR); [8, 8, 7, 5, 4, 3, 2]console.log (arr); [8, 8, 7, 5, 4, 3, 2]  Six, Connection array 1.concat () syntax: Arrayobject.concat (Array1,array2 ...) Function: Connect two or more arrays return values: New array   example var arr = [8,7,5,2,8,4,3];var Myarr = ["Y", "E", "w", "t"];var newArr = Arr.concat (Myarr); Console.log (NEWARR); [8, 7, 5, 2, 8, 4, 3, "Y", "E", "w", "T"]console.log (Myarr); ["Y", "E", "w", "T"]console.log (arr); [8, 7, 5, 2, 8, 4, 3]  VII, truncated array 1.slice () syntax: Arrayobject.slice (start,end) Function: Intercepts the selected element from the array return value: The new array parameter that is intercepted: Start (required): Specify where to start the selection, negative to start from the tail end (optional): Specify where to end the selection, is the array subscript at the end of the array; If end is not specified, the end from start to array   example var arr = [8,7,5,2,8,4,3];var newArr1 = Arr.slice (1, 3); var newArr2 = Arr.slice ( -3,-1); Console.log (NEWARR1); [7, 5]console.log (NEWARR2); [8, 4]console.log (arr); [8, 7, 5, 2, 8, 4, 3]  eight, find array 1.indexOf () syntax: Arrayobject.indexof (searchvalue,startindex) function: Start looking backwards from the beginning of the array (subscript 0), Judging method--Congruent return value: number, the position of the lookup item in the array (index value), the returned 1 parameter not found: Searchvalue (required): the element to find startindex (optional): The index of the starting position, not the default is 0  example: Var arr = [8,7,5,2,8,4,3];var newIndex1 = Arr.indexof (8); var newIndex2 = Arr.indexof (8,2); var newIndex3 = Arr.indexof (9); cons Ole.log (NEWINDEX1); 0console.log (NEWINDEX2); 4console.log (NEWINDEX3); -1 2.lastindexof () Syntax: Arrayobject.lastindexof (searchvalue,startindex) function: Look forward from the end of the array, judging the way--congruent return value: number, Find the position of the item in the array (index value), not found return-1 parameter: Searchvalue (required): the element to find startindex (optional): The index of the start position, not the default array last   example var arr = [ 8,7,5,2,8,4,3];vaR newIndex1 = Arr.lastindexof (8), var newIndex2 = Arr.lastindexof (8,2), var newIndex3 = Arr.lastindexof (9); Console.log ( NEWINDEX1); 4console.log (NEWINDEX2); 0console.log (NEWINDEX3); -1   Nine, delete, insert--splice1.splice ()--delete the specified element syntax at the specified location: Arrayobject.splice (Index,count) Function: Delete 0 or more elements that start at index: array, element parameters removed from the original array: index: The subscript of the starting position count: The number of elements to delete, not set, all   examples from index var arr = [ 8,7,5,2,8,4,3];var NEWARR = Arr.splice (1,3); Console.log (NEWARR); [7,5,2]console.log (arr); [8, 8, 4, 3] 2.splice ()--insert element syntax at specified position: Arrayobject.splice (index,count,item1,item2 ...) Function: Inserts one or more elements in the specified position return value: Array, element parameter removed from the original array: index: The subscript of the starting position count: The number of elements to delete, set to 0, do not delete the element item: the element to be inserted, plug in index   example var arr = [ 8,7,5,2,8,4,3];var NEWARR = Arr.splice (1,0, "item1", "item2"); Console.log (NEWARR); []console.log (arr); [8, "Item1", "item2", 7, 5, 2, 8, 4, 3] 3.splice ()--simultaneous insertion and deletion syntax: Arrayobject.splice (index,count,item1,item2 ...) Function: Inserts one or more elements in the specified position return value: Array parameter: Index: The subscript of the starting position count: The number of elements to be deleted item: the element to be inserted, the value returned before index: array, element removed from the original array   example var arr =[8,7,5,2,8,4,3];var NEWARR = Arr.splice ("Item1", "item2"); Console.log (NEWARR); [7,5]console.log (arr); [8, "Item1", "item2", 2, 8, 4, 3] 

JS Notes: Array method collation

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.