JavaScript Array Object instance method

Source: Internet
Author: User
Tags instance method javascript array

The JavaScript Array object instance methods are :pop (), push (), reverse (), shift (), sort (), splice (), Unshift (), concat (), join (), Slice ()

pop (): moves the last element in the array and returns the element. As follows:

var a=[1,2,3]; Console.log (A.pop ());    //  Console.log (a);   // output [+]

push (): adds an element to the tail of the array and returns the size of the array. As follows:

var a =[1,2,3];console.log (A.push (7));   // Output 4console.log (a);  //

reverse (): inverts the elements in the array and returns an inverted array. As follows:

var a=[1,2,3,4]; var b =//  [4,3,2,1]

shift (): removes the first element in an array and returns the element. As follows:

Console.log ([1,2,3,4].shift ());   //   1console.log ([4,5,6].shift ());   // 4

Sort (): sorts the array elements. Description: Method parameters are optional.

(1) If the method is called without parameters, the elements in the array are sorted alphabetically, more precisely, by the order in which the characters are encoded. To do this, you should first convert the elements of the array to a string, if necessary, for comparison. As follows:

Console.log ([1,2,3,4].sort ());   // [1,2,3,4]console.log ([3,2,4,1].sort ());   // [1,2,3,4]console.log ([' A ', ' B ', ' C ', ' d '].sort ());  // [' A ', ' B ', ' C ', ' d ']console.log ([' C ', ' a ', ' d ', ' B '].sort ());  // [' A ', ' B ', ' C ', ' d ']

(2) If you want to sort by other criteria, you need to provide a comparison function that compares two values and then returns a number that describes the relative order of the two values. The comparison function should have two parameters A and B with the following return values:

    • If a is less than B, a value that is less than 0 is returned if a should appear before B in the sorted array.
    • If a equals B, 0 is returned.
    • If a is greater than B, a value greater than 0 is returned.
var function (b) {    return A-B}; var nuar = [19,4,13,10,5,7,5,20]; Nuar.sort (SORTPROFN); Console.log (Nuar  ) ; // [4, 5, 5, 7, ten, a, +]

splice (): removes one or more elements from an array and, if necessary, inserts a new element at the location of the removed element, returning the element that was removed.  wherein the parameter description: Splice (ARG1,ARG2,ARG3.....,ARGN); The first two parameters arg1,arg2 are required, parameter arg3....argn: Optional, new item added to the array. Parameter arg1: Integer that specifies the location of the Add/remove item, using a negative number to specify the position from the end of the array. Parameter arg2: The number of items to delete. If set to 0, the item is not deleted

//only the first two parameters, splice (arg1, arg2), are deleted from the array (arg1+1) element, and the total number of deleted elements is arg2. Returns the element that was deleted. varA = [1,2,3,4];console.log (A.splice ());//[2,3]Console.log (a);//[1,4]Console.log ([1,2,3,4].splice (2,3));//[2,3,4]Console.log ([1,2,3,4].splice (2,20));//[2,3,4]//The arg3...argn parameter inserts a new element into the position of the removed element. varA = [5,6,7,8,9];console.log (A.splice (1,2,3,4));//[6,7]Console.log (a);//[5,3,4,8,9]varb = [5,6,7,8,9]console.log (B.splice (1,2,3,4,11,13,14));//[6,7]Console.log (b);//[5, 3, 4, one, 8, 9]

unshift (): adds a new element to the beginning of the array and returns the array size as follows:

var a=[1,2,3,4];console.log (A.unshift ());    // Output 4      (no arguments, no elements added) Console.log (a);   // output [1,2,3,4]Console.log (A.unshift (8));  // Output 5     (with parameters, insert element 8 to the beginning of the array.) )Console.log (a);   // output [8,1,2,3,4]

concat (): multiple array combinations, returning a new array after grouping. The arguments are multiple arrays to be grouped. As follows:

var a = [+]; var b = [2,3,4]; var c = A.concat (b); Console.log (c);    // [1,2,2,3,4] var d = C.concat ([7,6],[8,9],[11,12,13//[1,2,2,3,4,7,6,8,9,11,12,13] var e = [].concat ([0],[1],[2],[3],[4],[5],[6],[7],[8],[9//[ 0,1,2,3,4,5,6,7,8,9]

join (): puts all the elements of the array into a string. element is delimited by the specified delimiter. Return the string, as follows:

Console.log ([' A ', ' B ', ' C '].join ("|"));    // ' A|b|c '

Slice (): returns a new array of elements in the array. Select which element is controlled by the parameter. Description: This method does not modify the array and returns a new array. As follows:

// only one parameter is in the case (start). If a positive number, select the element (start+1) of the array and all subsequent elements. If it is a negative number, it specifies the position from the end of the array. In other words, 1 refers to the last element, 2 refers to the second-lowest element, and so on.  //[2,3,4,5]console.log ([7,8,9].slice ( -1));  // [9] // [8] // The parameter (start,end) is selected from the element (start+1) of the array to the end element of the array. Console.log ([1,2,3,4,5].slice (1,3));  // [2,3]

JavaScript Array Object instance method

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.