Concat (), slice (), splice ()

Source: Internet
Author: User

The Concat () method can create a new array based on all the items in the current array. Specifically, this method will first create a copy of the current array, then add the received parameters to the end of the copy, and finally return the newly constructed array. Without passing parameters to the Concat () method, It just copies the current array and returns a copy. If one or more arrays are passed to the Concat () method, this method adds each of these arrays to the result array. If the passed values are not arrays, these values are simply added to the end of the result array. As follows:

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

The slice () method creates a new array based on one or more items in the current array. The slice () method can take one or two parameters, that is, the start and end positions of the items to be returned. If there is only one parameter, the slice () method returns all items starting from the specified position of this parameter to the end of the current array. If there are two parameters, this method returns the item between the start position and the end position (but does not include the item at the end position ). Note that the slice () method does not affect the original array. As follows:

var colors = ["red", "green", "blue", "yellow", "purple"];var colors2 = colors.slice(1);var colors3 = colors.slice(1, 4);alert(colors2);     //green,blue,yellow,purplealert(colors3);     //green,blue,yellow

The main purpose of splice () is to insert an entry into the middle of the array, but there are three ways to use this method.

Delete: You can delete any number of items. You only need to provide two parameters: the location of the first item to be deleted and the number of items to be deleted. For example, splice () deletes the first two items in the array.

Insert: you can insert any number of items. You only need to provide three parameters: Start position, 0 (number of items to be deleted), and the items to be inserted. If you want to insert multiple items, You can import the fourth and fifth items to any number of items. For example, splice (, "Red", "green") inserts strings "red" and "green" starting from position 2 of the current array ".

Replace: you can insert any number of items and delete any number of items at the same time. You only need to provide three parameters: Start position, number of items to be deleted, and any number of items to be inserted. The number of inserted items does not have to be equal to the number of deleted items. For example, splice (, "Red", "green") deletes the entry in position 2 of the current array, and then inserts the string "red" and "green" starting from position 2 ".

The splice () method always returns an array that contains items deleted from the original array (if no items are deleted, an empty array is returned ).

VaR colors = ["red", "green", "blue"]; var removed = colors. splice (); // Delete the first alert (colors); // green, bluealert (removed); // red, the returned array contains only one removed = colors. splice (, "yellow", "orange"); // insert two alert (colors) items starting from position 1; // green, yellow, orange, bluealert (removed ); // The returned result is an empty array removed = colors. splice (, "Red", "Purple"); // insert two items to delete an alert (colors); // green, red, purple, orange, bluealert (removed); // yellow, the returned array contains only one

 

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.