Home > Web Front-end > JS Tutorial > body text

Differences between splice slices_Basic knowledge

WBOY
Release: 2016-05-16 19:25:25
Original
1180 people have browsed it

1. slice method (Array)
returns a segment of an array.

arrayObj.slice(start, [end])

Parameters
arrayObj

Required. An Array object.

start

Required. The starting element of the specified portion of arrayObj is a zero-based index.

end

Optional. The end element of the portion specified in arrayObj is the zero-based index.

Description
The slice method returns an Array object, which contains the specified part of arrayObj. The

slice method copies up to, but not including, the element specified by end. If start is negative, treat it as length start, where length is the length of the array. If end is negative, it is treated as length end, where length is the length of the array. If end is omitted, the slice method will copy until the end of arrayObj. If end appears before start, no elements are copied to the new array.

Example
In the following example, all elements in myArray except the last element are copied to newArray:

newArray = myArray.slice(0, - 1)
2. The splice method
removes one or more elements from an array, inserts a new element at the position of the removed element if necessary, and returns the removed element.

arrayObj.splice(start, deleteCount, [item1[, item2[, . . . [,itemN]]]])

Parameters
arrayObj

Required options. An Array object.

start

Required. Specifies the starting position to remove elements from the array. This position is calculated starting from 0.

deleteCount

Required. The number of elements to remove.

item1, item2,. . .,itemN

Required. A new element to be inserted at the location of the removed element.

Explanation
The splice method can modify arrayObj by removing a specified number of elements starting from the start position and inserting new elements. The return value is a new Array object consisting of the removed elements.

Requires
Version 5.5

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!