How to create an array object and how to do it

Source: Internet
Author: User

First, create an array, that is, instantiate an array object in three ways: 1, new Array (); 2, New Array ( size); 3, New Array ( element0, element1, ..., ELEMENTN);     Second, the commonly used methods of operation 1, Concat () method is used to connect two or more arrays, the method does not change the existing array, but will only return the concatenated new array; Arrayobject.concat (Arrayx,arrayx,......, arrayx): Arrayx The parameter can be a specific value, or it can be an array object, can be any number;     If the argument for the concat () operation is an array, then the element in the array is added, not the array;          For example: 2, the Join () method is used to put all the elements in the array into a string, so the result is to return a string concatenated with a connector; Arrayobject.join ( Separator); Separator to specify the delimiter to use, if omitted, use a comma as the delimiter;      For example:           3, Pop () method for deleting In addition to returning the last element of the array, that is, the original array changes;          arrayobject.pop (); Note: If the array is already empty, the pop () Do not change the array and return the undefined value;     :           4, push () method adds one or more elements to the end of the array and returns a new long The original array will vary;          arrayobject.push (Newelement1,newelement2,...., NEWELEMENTX); The first parameter is required, and subsequent parameters are optional;     :                5, re The verse () method is used to reverse the order of the elements in the array; The method changes the original array without creating a new array;          arrayobject.reverse ();      : The                6, Shift () method is used to remove the first element of the array and return the value of the first element, which will change the original array;  & nbsp;       arrayobject.shift (); Note: If the array is empty, then the shift () method will not take any action and return the undefined value;      :     &NBsp The          7, Slice () method returns the selected element from an existing array, without changing the original array;           arrayobject.slice (start,end);                    parameter start: Required, specifies where to start the selection, and if it is negative, it specifies the position from the end of the array, that is, 1 refers to the last element, 2 refers to the second-lowest element, and so on;                    parameter end: optional, specifying where to end the selection, which is the array subscript at the end of the array fragment, or if the parameter is not specified, the segmented array contains all elements from start to the end of the array. If this parameter is a negative number, it specifies the element starting from the end of the array;          return value: Returns a new array containing elements from the start to ends (excluding the element) from the Arrayobject;      :                8, sort () method for sorting the elements of an array; Arrayobject. Sort ( SortBy); SortBy: Optional, specify the sort order, must be a function;                    Note: If the method is called without parameters, it is sorted by character encoding; To do this, the elements of the array should first be converted to strings (if necessary) for comparison. If you want to sort by other criteria, you need to provide a comparison function that compares two values and 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: A,If A is less than B, in the sorted array A should appear before B, then a value less than 0 is returned;B,If a equals B, 0 is returned; C, if a is greater than B, returns a value greater than 0;     :                   &NBSP ;       9, Splice () method adds/deletes items to/from the array, and then returns the deleted item; This method changes the original array;          arrayobject.splice (index,howmany,item1,....., ItemX) ;                Parameters index: required, integers, specify where to add/remove items, use negative numbers to specify positions from the end of the array;                parameter howmany: required, number of items to be deleted, if set to 0, the item will not be deleted;                Parameters item1,..., ItemX: Optional, new items added to the array;          Description: The splice () method deletes 0 or more elements starting at index and uses the sound in the argument list The deleted element, and if the element is removed from Arrayobject, the array containing the deleted element is returned;     :          &NB The sp;     10, toString () method converts an array to a string and returns the result;           Arrayobject.tostring (); The return value is the same as the string returned by the join () method without parameters;     11,toLocaleString () converts an array to a local string;          arrayobject.tolocalestring (); The toLocaleString () method of each array element is called first, and then the resulting string is concatenated using a locale-specific delimiter to form a string;         :                    12, Unshift () methods you can add one or more elements to the beginning of the array and return the new length; Note: I E does not work properly;          arrayobject.unshift (Newelement1,newelement2,...., NEWELEMENTX);                parameter newelement1: required, first element added to array;      &N Bsp         parameter newelement2: optional, second element added to array;                Parameters Newelem Entx: Optional, can add several elements;         :                

How to create an array object and how to do it

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.