Slice () method and Splice () difference in native JS

Source: Internet
Author: User

Both the slice () method and the splice () method are the methods of the array operation in native JS.

Slice () returns a new array that returns the selected element from an existing array. For example: Arrobject (Start,end), start is required. Specifies where to start the selection, and if it is a negative number, it is selected from the end of the array element, that is, 1 refers to the last element, 2 refers to the second-lowest element, and end is an optional element. Specifies where to end the selection. This parameter does not indicate that the end of the array is intercepted from the beginning and, if it is negative, that the element is intercepted forward from the end of the array. The method does not modify the original array, and if you want to delete an element of an array, use the splice () method.

Splice (), add/Remove elements from the array. For example: Arrayobject.splice (index,howmany,item1,....., ItemX). Index is required to specify the location of the Add/remove item. The Howmany must be an entry that represents the number of deleted items, and if 0 indicates that the element is not deleted. Splice () removes Howmany elements starting with index and can replace deleted elements with the item element. The item optional parameter, which represents the newly added item.

Usage such as:

(1):

var arr = new Array (5);
Arr[0] = "Amy";
ARR[1] = "Elice";
ARR[2] = "divi";
ARR[3] = "Lvy";
Arr[4] = "Marry";
Arr.splice (1, 0, "Willian");
Console.log (arr);
Output: Amy,willian,elice,divi,lvy, adding an element at the first position of the array with a value of "Willian"
(2):
var arr = new Array (5);
Arr[0] = "Amy";
ARR[1] = "Elice";
ARR[2] = "divi";
ARR[3] = "Lvy";
Arr[4] = "Marry";
Arr.splice (1, 2, "Willian");
Console.log (arr);
Output: Amy,willian,lvy, removes two elements from the first position of the array, and replaces the deleted element with the new element "Willian".

Slice () method and Splice () difference in native JS

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.