Operation of Arrays (array)

Source: Internet
Author: User

1, concat () creates a new array based on all the items in the current array.

Specifically, this method creates a copy of the current array, adds the received parameters to the end of the copy, and returns the newly constructed array. In the case where no arguments are passed to the concat () method, it simply copies the current array and returns a copy, and if passed to the Concat () method is one or more arrays, the method adds each item in those arrays to the result array. If the pass is not an array, these values are simply added to the end of the result array.

var colors = ["Red", "green", "Blue"]; var colors2 = colors. concat ("Yellow", ["Black", "Brown"]); alert (colors);    // red,green,bluealert (colors2);  // Red,green,blue,yellow,black,brown

2, Slice () creates a new array based on one or more items in the current array.

The slice () method can accept one or two parameters, that is, to return the starting and ending positions of an item.

In the case of only one argument , the slice () method returns all items starting at the specified position of the parameter to the end of the current array.

If there are two parameters , the method returns the entry between the start and end positions, but not the ending position .

Note that the slice () method does not affect the original array. Take a look at the following example:

var colors = ["Red", "green", "blue", "yellow", "brown"]; var colors2 = colors. Slice (1); var colors3 = colors. Slice (1, 3); alert (colors2);   // green, blue, yellow, brownalert (COLORS3);  // Green, Blue

If the slice () method has a negative number in its argument, the array length is used to determine the corresponding position. For example, calling Slice (-2,-1) on an array containing 5 items is the same as calling Slice (3, 4). If the end position is less than the start position, an empty array is returned.

Operation of Arrays (array)

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.