JavaScript Slice () and splice () usage

Source: Internet
Author: User
Tags in python


JavaScript Slice () method

Definitions and usage

The slice () method returns the selected element from an existing array.

Grammar
Arrayobject.slice (start,end) parameter description
Start Required. Specify where to start the selection. If it is a negative number, then it sets the position from the end of the array. In other words,-1 refers to the last element, 2 to the penultimate element, and so on.
End is optional. Specify where to end the selection. The parameter is the array subscript at the end of the array fragment. If this argument is not specified, the Shard array contains all the elements from start to the end of the array. If this argument is a negative number, it sets the element that starts at the end of the array.

return value

Returns a new array containing the elements in the arrayobject from start to end (excluding the element).

Description

Note that the method does not modify the array, but instead returns a child array. If you want to delete a section of an element in an array, you should use the method

Array.splice ().

Tips and comments

Note: You can use negative values to select elements from the tail of the array.

Note: If end is not specified, then the slice () method selects all elements from start to ending of the array.
JavaScript Splice () method
Definitions and usage
The splice () method inserts, deletes, or replaces the elements of an array.

Grammar
Arrayobject.splice (index,howmany,element1,....., elementx) parameter description
Index required. Specify where to add/remove elements from.

This parameter is the subscript for the array element that starts inserting and/or deleting, and must be a number.

Howmany required. Specify how many elements should be deleted. Must be a number, but it can be "0".

If this parameter is not specified, all elements that start from index to the end of the original array are deleted.

Element1 Optional. Specify the new element to add to the array. Start the insertion at the lower mark that is indicated by index.
Elementx Optional. You can add several elements to an array.

return value
If an element is removed from the Arrayobject, an array containing the deleted elements 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.

-------------------------------------------------------------------------------

It's all my copy, and my own understanding is that the slice and Python fragments are the same, without changing the original array (list) argument to be negative. But the beginning of a slice in Python, the end can be empty. JavaScript Slice start is a must.

Splice a way to insert, delete, and replace elements of the original array. There is no such a single method in Python, only insert,remove,del or something.


Splice and slice methods to manipulate arrays, very useful two methods.

Splice syntax

Arrayobject.splice (index,howmany,element1,....., elementx)

Splice description

To delete the array elements, modify the original array, starting from index (based on 0 index) Delete howmany elements, delete after the index after adding element1....elementx elements, in addition to index, other parameters optional, index can be negative, Negative number then it prescribes the position from the end of the array. In other words,-1 refers to the last element, 2 to the penultimate element, and so on.

Slice syntax

Arrayobject.slice (Index,howmany)

Slice description

Slice the array element, does not modify the original array, returns a howmany array element that starts with the index (based on 0 index), and returns a new, tangent. Index is a required parameter, Howmany is an optional parameter, and if index is a negative number, it stipulates the position from the end of the array. In other words,-1 refers to the last element, 2 to the penultimate element, and so on.

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.