Object-oriented knowledge js-built-in objects (emphasis)--array

Source: Internet
Author: User

Array combination memory shift unshift POP push

Add and remove
Shift Unshift from the beginning of the array to add or remove
Pop push is added or removed from the end of the array

shift: Deletes the first item of the original array and returns the value of the deleted element; returns undefined if the array is empty

    var arr1 = [1,2,3,4,5];    var result = Arr1.shift (); a:[2,3,4,5] B:1

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); 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.

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 (); 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

    ARR1 = [1,2,3,4,5];    result = Arr1.push (6,7); a:[1,2,3,4,5,6,7] B:7

concat Array Merging

   Concat: Returns a new array, which is the    arr1 = [1,2,3,4,5] That is added to the original array;    result = Arr1.concat (6,7); a:[1,2,3,4,5] b:[1,2,3,4,5,6,7]

Sort the sort from small to large reverse from big to low

    ARR1 = [1,2,3,4,5];    result = Arr1.sort (); [1,2,3,4,5]    arr1 = [1,2,3,4,5,8,34];    result = Arr1.reverse (); [34,8,5,4,3,2,1]

Slice (start,end) Array interception

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 position

    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;

Join (separator) Arrays are stitched into strings

    Sets the element group of the array as a string, separator as a delimiter, or 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"

Splice

Splice (Start,deletecount,val1,val2,...) :
Delete the DeleteCount entry starting from the start position and insert the val1,val2 from that position,...

Array extension Method---Remove duplicates in array

Array.prototype.unique = function () {        var a = []; var l = this.length;        for (var i = 0, i < l; i++)        {for            (var j = i + 1; j < L; j + +)            {                if (this[i] = = = This[j]) j = ++i;
   }            A.push (This[i]);        }        return A;    };    var arr = [1,2,3,4,5,6,6,7,7,77,7,]    console.log (Arr.unique ())

  

 

Object-oriented knowledge js-built-in objects (emphasis)--array

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.