JavaScript QuickStart-ecmascript Local objects (Array)

Source: Internet
Author: User
Tags array length sorts

Array Object

The array object, like the list object inside Python, is an object that stores multiple object values, and the methods and properties are basically similar.

First, the attribute

Lenght

Second, the method

1, concat () is used to connect two or more arrays. Similar to the Extend method in Python.

Arrayobject.concat (Arrayx,arrayx,......, Arrayx)

2, join () is used to put all the elements in the array into a string. Similar to join in Python. ' * '. Join (a)

Join usage in javascript:

3, Pop () is used to delete and return the last element of the array. Same as the pop () in Python.

4. Push () adds one or more elements to the end of the array and returns the new length.

5, reverse () reverses the order of the elements in the array.

6, Shift () Delete and return the first element of the array

7. Slice () returns the selected element from an existing array

Parameter: Slice (start,end) End is not specified, default is start to end.

Note: Both start and end can be negative.

8. Sort () Sorts the elements of an array

arrayobject. Sort (sortby) #参数sortby可选, which specifies the sort order, must be a function.

Similar to the sorted function in Python. For example dic={' name ': ' Xuequn ', ' age ': +, ' sex ': 1}  sorted (Dic.items (), Key=lambda x:x[1])  #以字典的键排序

JavaScript is the same. The default sort () function sorts the letters of an element, which is the ASCII code. But we can also define our own functions. For example:

9, Splice () Add/remove items from the array, and then return the deleted items. Note that the splice () method works differently than the slice () method, and the splice () method modifies the array directly.

Splice (INDEX,NUM,ITEM1,ITEM2....ITEMN) #从index开始, followed by NUM elements, replaced with item1.item2 .... Itemn.

For example:

10, Tosource () represents the source code of the object. Only Gecko core browsers (such as Firefox) support this method, ie, Safari, Chrome, Opera and other browsers do not support this method.

11. The toString () method converts an array to a string and returns the result.

12, Tolocalstring) () converts the array to a local string. The toLocaleString () method of each array element is called first, and the resulting string is concatenated using a locale-specific delimiter to form a string.

13, Unshift () adds one or more elements to the beginning of the array and returns the new length.

14. The ValueOf () method returns the original value of the Array object.

Iii. Summary

There are several main methods of array:

1, increase
    • Concat (): Adds one or more elements, modifies the array directly, and returns the new array.
    • Push (): Adds 1 or more elements to the end of the array, returning a new array length.
    • Unshift (): Adds 1 or more elements at the beginning, returning the new array length.
2. By deleting
    • Pop (): Deletes the last element. Returns the last element value.
    • Shift (): Deletes the first element. Returns the value of the first element.
3, change
    • Splice (): Adds/Deletes an item in an array, and then returns the element that was deleted.   Splice (Index,num,item ...) Action element: ordinal index element; Num: Delete number, add element for 0 o'clock; Item is the new element.
4. Check
    • Slice (start,end): Slice gets a subset within an element. When end is empty, the default is to the end.
5, Hopewell
    • Join (): joins the array elements according to the specified characters.
6. Preface
    • Sort (): order, accept function as parameter.
    • Reverse (): Invert




JavaScript QuickStart-ecmascript Local objects (Array)

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.