Javascript Array Operations

Source: Internet
Author: User
Tags javascript array
Shift: Delete the first entry of the original array and return the value of the deleted element. If the array is empty, return undefined var a = [1, 2, 3, 4, 5]; var B =. shift (); // a: [2, 3, 4, 5] B: 1 unshift: add the parameter to the beginning of the original array, and return the array length var a = [1, 2, 3, 4, 5]; var B =. unshift (-2,-1); // a: [-2,-,] B: 7 Note: In IE6.0, the returned value is always undefined, the test return value in FF2.0 is 7, so the return value of this method is not reliable. 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, return undefined var a = [1, 2, 3, 4, 5]; var B =. pop (); // a: [,] B: 5 push: add the parameter to the end of the original array, and return the array length var a =, 5]; var B =. push (6, 7); // a: [1, 3, 4, 5, 6, 7] B: 7 concat: returns a new array, is to add parameters to the original array to form var a = [1, 2, 3, 4, 5]; var B =. concat (); // a: [, 5] B: [, 7] slice () was originally used to capture a part of the array, here I use it to copy the array. Its format is as follows: array. slice (start, end) if the end parameter is omitted, the split array contains all elements from start to end of the array. Now we need to use it to copy the array, just one line: var newArray = oldArray. slice (0); 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 =. splice (, 9); // a: [,] B: [] var B =. splice (0, 1); // same as shift. splice (0, 0,-2,-1); var B =. length; // same as unshift var B =. splice (. length-1, 1); // same as pop. splice (. length, 0, 6, 7); var B =. length; // same as push reverse: returns the reverse order of the array var a = [1, 2, 3, 4, 5]; var B =. reverse (); // a: [5, 4, 3, 2, 1] B: [5, 4, 3, 2, 1] sort (orderfunction ): sort the array by the specified parameter var a = [1, 2, 3, 4, 5]; var B =. sort (); // a: [1, 2, 3, 4, 5] B: [1, 2, 3, 4, 5] slice (start, end ): returns the array var a = [1, 2, 4, 5]; var B =. slice (); // a: [, 5] B: [, 5] join (separator): groups the elements of the array into a string, use separator as the separator, otherwise use the default comma as the separator var a = [1, 2, 4, 5]; var B =. join ("|"); // a: [1, 2, 3, 4, 5] B: "1 | 2 | 3 | 4 | 5"
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.