Javascript array operation function summary, javascript Array Function

Source: Internet
Author: User

Javascript array operation function summary, javascript Array Function

In fact, usually a lot of push and pop should be used, but it should be noted down for later use.

Shift: Delete the first entry of the original array and return the value of the deleted element. If the array is empty, undefined is returned.

Copy codeThe Code is as follows:
Var a = [1, 2, 3, 4, 5];
Var B = a. shift (); // a: [2, 3, 4, 5] B: 1

Unshift: Add the parameter to the beginning of the original array and return the length of the array.

Copy codeThe Code is as follows:
Var a = [1, 2, 3, 4, 5];
Var B = a. unshift (-2,-1); // a: [-2,-,] B: 7

Note: In IE6.0, the test return value is always undefined, and in FF2.0, the test return value is 7. Therefore, the return value of this method is unreliable. You need to use splice instead of this method when returning the value. This article is from www.45it.com

Pop: Delete the last entry of the original array and return the value of the deleted element. If the array is empty, undefined is returned.

Copy codeThe Code is as follows:
Var a = [1, 2, 3, 4, 5];
Var B = a. pop (); // a: [1, 2, 3, 4] B: 5

Push: Add the parameter to the end of the original array and return the length of the array.

Copy codeThe Code is as follows:
Var a = [1, 2, 3, 4, 5];
Var B = a. push (6, 7); // a: [1, 2, 3, 4, 5, 6, 7] B: 7

Concat: Returns a new array consisting of adding parameters to the original array.

Copy codeThe Code is as follows:
Var a = [1, 2, 3, 4, 5];
Var B = a. concat (6, 7); // a: [1, 2, 3, 4, 5] B: [1, 2, 3, 4, 5, 7]

Splice(Start, deleteCount, val1, val2,...): Delete the deleteCount item from the start position, and insert val1, val2 ,...

Copy codeThe Code is as follows:
Var a = [1, 2, 3, 4, 5];
Var B = a. splice (, 9); // a: [,] B: []
Var B = a. splice (0, 1); // same as shift
A. splice (0, 0,-2,-1); var B = a. length; // same as unshift
Var B = a. splice (a. length-1, 1); // same as pop
A. splice (a. length, 7); var B = a. length; // same as push

---------------------------------------------------

Splice details:

The splice function removes one or more elements from an array. If necessary, insert a new element to the position of the removed element and return the removed element.

ArrayObj. splice (start, deleteCount, [item1 [, item2 [,... [, itemN])

ArrayObj is required. An Array object.

Start is required. Specifies the start position of the element to be removed from the array. This position is calculated from 0.

DeleteCount is required. The number of elements to be removed.

Item1, item2,..., itemN is required. The new element to be inserted at the position of the removed element.

In JavaScript, the splice function can remove a specified number of elements starting from the start position and insert new elements to modify arrayObj. The returned value is a new Array object consisting of the removed elements.

---------------------------------------------------

Reverse: Returns the reverse order of arrays.

Copy codeThe Code is as follows:
Var a = [1, 2, 3, 4, 5];
Var B = a. reverse (); // a: [5, 4, 3, 2, 1] B: [5, 4, 3, 2, 1]

Sort (orderfunction ):Sorts arrays by specified parameters

Copy codeThe Code is as follows:
Var a = [1, 2, 3, 4, 5];
Var B = a. sort (); // a: [1, 2, 3, 4, 5] B: [1, 2, 3, 4, 5]

Slice (start, end ):Returns a new array consisting of items from the specified start subscript to the end subscript in the original array.

Copy codeThe Code is as follows:
Var a = [1, 2, 3, 4, 5];
Var B = a. slice (); // a: [, 5] B: [, 5]

Join (separator ):Groups the elements of the array into a string with the separator. If this parameter is omitted, the Delimiter is separated by commas by default.

Copy codeThe Code is as follows:
Var a = [1, 2, 3, 4, 5];
Var B = a. join ("|"); // a: [1, 2, 3, 4, 5] B: "1 | 2 | 3 | 4 | 5"

The above is all the content of this article. I hope my friends will like it.

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.