Day 191th: JS---Array common properties and methods summary

Source: Internet
Author: User

Array---Common properties and methods summary 1, Array object constructor
1 /*Array Object Constructors*/2 3     /*Combination Memory shift unshift POP push4 Add and remove5 shift Unshift from the beginning of the array to add or remove6 pop push is added or removed from the end of the array7      */8 9     //Shift: Deletes the first item of the original array and returns the value of the deleted element; returns undefined if the array is emptyTen     varARR1 = [1,2,3,4,5]; One     varARR2 = [1,2,3,4,5]; A     varresult =arr1. Shift ();  -Console.log (result);//a:[2,3,4,5] B:1 -  the     //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);  -Console.log (result);//a:[-2,-1,1,2,3,4,5] B:7 +     //Note: The Test return value under IE6.0 is always undefined, -     //FF2.0 The return value of the test is 7, so the return value of this method is not reliable, need to use the return value when the splice instead of this method can be used.  +  A  at     //pop: Deletes the last item of the original array and returns the value of the deleted element ; returns undefined if the array is empty -arr1= [1,2,3,4,5]; -result =arr1. Pop ();  -Console.log (result);//a:[1,2,3,4] B:5 -  -     //push: Adds a parameter to the end of the original array and returns the length of the array  inARR1 = [1,2,3,4,5]; -result = Arr1.push (6,7);  toConsole.log (result);//a:[1,2,3,4,5,6,7] B:7
2. Array Merging---concat
1 /* array Merge Concat */ 2 3     // concat: Returns a new array that consists of adding parameters to the original array 4     arr1 = [1,2,3,4,5]; 5     result = Arr1.concat (6,76     console.log (result);   a:[1,2,3,4,5] b:[1,2,3,4,5,6,7]
3, array sorting---sortingSortFrom small to large,ReverseFrom big to small
1 /*sort the sort from small to large reverse from big to low*/2      //sort (orderfunction): Sorts the array by the specified parameters3ARR1 = [1,2,3,4,5];4result =arr1. Sort (); 5Console.log (result);//[1,2,3,4,5]6 7     //Reverse: Reverse the array8ARR1 = [1,2,3,4,5,8,34];9result =arr1. Reverse (); TenConsole.log (result);//[34,8,5,4,3,2,1]
4. Array interception---Slice(Start,End)
1 //Slice (start,end): Returns a new array of entries from the original array that specifies the starting subscript to the end of the index, but does not include the item at the end position2ARR1 = [1,2,3,4,5];3result = arr1.Slice(2,5);//[3,4,5]4result = 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;5 6     //return to itself7result = Arr1.slice (0);//return to itself
5. The array is stitched into a string---Join(Separator)
1 /* Join (separator) arrays are stitched into strings */ 2     // sets the element group of the array as a string, separator as a delimiter, and the default comma delimiter if omitted 3     arr1 = [1,2,3,4,5]; 4     result = arr1. Join // a:[1,2,3,4,5] B: "1|2|3|4|5"
6, Splice
1 /*Splice*/2     //Splice (Start,deletecount,val1,val2,...) :3     //Delete the DeleteCount entry starting from the start position and insert the val1,val2 from that position,...4ARR1 = [1,2,3,4,5];5result = arr1.Splice(2,2,7,8,9);//a:[1,2,7,8,9,5] b:[3,4]6result = arr1.Splice(0,1);//with Shift7 8Arr1.Splice(0,0,-2,-1);9result = Arr1.length;//with UnshiftTen  Oneresult = arr1.Splice(arr1.length-1,1);//with pop AArr1.Splice(arr1.length,0,6,7); -result = Arr1.length;//with Push
7. Remove duplicates from the array
1<script>2     /*-- Array extension Method- -*/3     /*4 * Remove duplicates from the array5      */6Array.prototype. Unique =function() {7         vara = [];varL = This. Length;8          for(vari = 0; I < L; i++)9         {Ten              for(varj = i + 1; J < L; J + +) One             { A                 if( This[i] = = = This[j]) J = + +i; -             } -A.push ( This[i]); the         } -         returnA; -     }; -  +     vararr = [1,2,3,4,5,6,6,7,7,77,7,] -Console.log (arr.Unique());//Array (8) +  A</script>

Day 191th: JS---Array common properties and methods summary

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.