Usage of splice () in JS

Source: Internet
Author: User

Instance

Move the third element of the divisor group and add the new element in the third position of the array:

var fruits = ["Banana", "Orange", "Apple", "Mango"];
Fruits.splice (2,1, "Lemon", "Kiwi");

Fruits Output results:

Banana,orange,lemon,kiwi,mango Definition and usage

The splice () method is used to insert, delete, or replace elements of an array.

Note: This method will change the original array!.

Browser support

All major browsers support splice ().

Grammar Array. Splice ( Index, Howmany, item1,....., ItemX) parameter values
Parameters 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.
item1, ..., ItemX Optional. The new element to add to the array
return value

Type Description
Array If the element is removed from the Arrayobject, the array containing the deleted element is returned.

Usage of splice () 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.