Summary of common methods for confusing the array of articles

Source: Internet
Author: User
Tags array sort

Serial number Method Example Whether to change the current array
1 Instanceof to determine if a variable is an array object
Console.log (arr instanceofarray);//true
2  join    Sets the element group of an array as a string   join (separator)  //a string of elements in the array, separator as a delimiter, and omitted by default with a comma delimiter ARR1 = [1,2,3,4,5]; result = Arr1.join ("|");//a:[1,2,3,4,5] B: "1|2|3|4|5" no
3 push   add an item to the end of the array // Push: Adds a parameter to the end of the original array and returns the length of the array ARR1 = [1,2,3,4,5]; result = Arr1.push (6,7);//a:[1,2,3,4,5,6,7] B:7 console.log (Result) Yes
4 pop    the last item in the popup array // Pop: Deletes the last item of the original array and returns the value of the deleted element, or returns the undefined arr1= when the array is empty; 1,2,3,4,5]; result = Arr1.pop ();//a:[1,2,3,4] B:5 console.log (Result)// 5 Yes
5 Reverse flipping an array ///Reverse: Reverse the array arr1 = [1,2,3,4,5,8,34]; result = Arr1.reverse ();//[34,8,5,4,3,2,1] Console.log (result); Is
6 Sort order--the internal implementation of the simulation sort Sort ();//Even the array sort is also based on characters, from small to large sort with parameters how is it implemented? Alphabetical First letter ASCII code, small in front, sort does not have descending function, arr.reverse ();//order and reverse, into descending arr.sort (function (x, y) {return Y-x;} );//numeric descending arr.sort (function (x, y) {return x-y;}); /digit Ascending //per item length
Arr.sort (function (b) {
return a.length-b.length;
})//from small to large
Console.log (arr);
7  slice   intercepts a new array from the current array, without affecting the original array   slice (start,end): Parameter start starting from 0, end starting from 1   Returns a new array of entries from the original array that specify the starting subscript to the end of the subscript, but excluding the entry at the end position   Span style= "FONT-SIZE:14PX; Color: #000000; " >ARR1 = [1,2,3,4,5]; result = Arr1.slice (2,5);//[3,4,5] result = Arr1.slice (2); [3,4,5] In the case of only one argument, the slice () method returns all items starting at the specified position of the parameter to the end of the current array;   no
8 Splice Delete or replace some items of the current array, affecting the original array to return the deleted content  splice ()//delete or replace some items of the current array,Splice (Start,deletecount,val1,val2,...) ://Delete the DeleteCount entry starting from the start position and insert the val1,val2 from that position,...  //Insert Var arr=[1,2,3,4 before the first item]Arr.splice (1,0,5);
 //Replace ARR1 = [1,2,3,4,5];result = Arr1.splice (2,2,7,8,9);//a:[1,2,7,8,9,5] b:[3,4]//Delete First result = Arr1.splice (0,1);//Same Shift Arr1.splice (0,0,-2,-1);result = arr1.length;//With Unshift result = Arr1.splice (arr1.length-1,1);//with pop//Add Arr1.splice (arr1.length,0,6,7);
9 indexof (), lastIndexOf ()    Gets the position of an item in the array, returns 1 lastindexof ()//start retrieving indexof (Item,startindex)//The first parameter represents the element that needs to be looked up, the second parameter represents the position to start retrieving, and the second argument is not written, indicating that the no
10    arr = arr.concat ("xx", "yy"); array merging concat //concat: Returns a new array that is a that is a parameter added to the original array. ARR1 = [1,2,3,4,5]; result = Arr1.concat (6,7); console.log (Result)//[1,2,3,4,5,6,7] //the method expands the one-dimensional array in the parameter result = Arr1.concat (6,7,[8,9,10]); result = Arr1.concat (6,7,[8,9,10,[1,2,3]]);//1.2.3 does not expand no
11 ToString ()//Convert the array to a string, each with, split var str=arr.tostring (); Whether
12 ValueOf ()//Returns the array object itself Whether
13 Filter array IE8 not previously supported
14 ForEach traversal array IE8 not previously supported
15 shift ()//Remove the first element in the array, modify the length property of the original array, shift: Deletes the first item of the original array and returns the value of the deleted element; returns undefined var arr1 = [1,2,3,4,5]; var arr2 = [1,2,3,4,5]; var result = Arr1.shift ();//arr1 [2,3,4,5]  result:1 Span style= "FONT-SIZE:14PX; Color: #000000; " >console.log (Result) Yes
16 Unshift ()//insert item at the top of the array, return the length of the array //unshift: Adds a parameter to the beginning of the original array and returns the length of the array arr1 = [1,2,3,4,5]; result = Arr1.unshift ( -2,-1);//arr1:[-2,-1,1,2,3,4,5] Result:7 Console.log (Result) Is

Summary of common methods for confusing the array of articles

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: 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.