JS Array Operations

Source: Internet
Author: User

 

// 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.
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.
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.

// 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.
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.
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.
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,
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

// Reverse: returns the reverse order of the array.
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.
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.
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 that uses the separator. If this parameter is omitted, a comma is used as the Separator by default.
VaR A = [1, 2, 3, 4, 5];
VaR B = A. Join ("|"); // A: [1, 2, 3, 4, 5] B: "1 | 2 | 3 | 4 | 5"

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.