The splice method and slice method in JS

Source: Internet
Author: User

I. Definition and usage
The splice () method is used to insert, delete, or replace elements of an array.
Grammar
Arrayobject.splice (index,howmany,element1,....., elementx)
Parameter description
Index
Necessary. Specifies where to add/remove elements from.
This parameter is the subscript that begins inserting and/or deleting an array element, which must be a number.
Howmany
Necessary. Specifies how many elements should be deleted. Must be a number, but it can be "0".
If this parameter is not specified, all elements from index start to the end of the original array are deleted.
Element1 is optional. Specifies the new element to be added to the array. Start the insertion from the subscript at index point.
Elementx is optional. You can add several elements to an array.
return value
If the element is removed from the Arrayobject, the array containing the deleted element is returned.
Description
The splice () method deletes 0 or more elements starting at index and replaces the deleted elements with one or more values declared in the argument list.
Hints and Notes
Note: Notice that the splice () method works differently than the slice () method, and the splice () method modifies the array directly.

Ii. Definition and usage
The slice () method extracts a portion of a string and returns the extracted part with a new string.
Grammar
Stringobject.slice (Start,end)
Parameter description
Start subscript of the fragment to extract. If it is a negative number, the parameter specifies where to start from the end of the string. That is,-1 refers to the last character of the string, 2 refers to the second-lowest character, and so on.
End subscript at the end of the fragment to be extracted immediately. If this parameter is not specified, the substring to be extracted includes the string from start to the end of the original string. If the parameter is negative, it specifies the position from the end of the string.
return value
A new string. Includes the string Stringobject all characters from start (including start) to end (not including end).
Description
The methods of the String object Slice (), substring (), and substr () (not recommended) can return the specified part of the string. Slice () is more flexible than substring () because it allows negative numbers to be used as arguments. Slice () differs from substr () because it specifies a substring with a position of two characters, and substr () specifies a substring with a character position and length.
Also note that String.slice () is similar to Array.slice ().

The splice method and slice method in JS

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.