JS uses array. splice to implement insert/remove of Array

Source: Internet
Author: User

Arrayobj. splice (START, deletecount, [Item1 [, item2 [,... [, itemn])
Required. An array object.
Required. Specifies the start position of the element to be removed from the array. This position is calculated from 0.
Required. The number of elements to be removed.
Item1, item2,..., itemn
Required. The new element to be inserted at the position of the removed element.
The splice method can remove the specified number of elements starting from the start position and insert new elements to modify arrayobj. The returned value is a new array object consisting of the removed elements.
Release 5.5 CopyCode The Code is as follows: array. Prototype. Clear = function (){
This. Length = 0;
Array. Prototype. insertat = function (index, OBJ ){
This. splice (index, 0, OBJ );
Array. Prototype. removeat = function (INDEX ){
This. splice (index, 1 );
Array. Prototype. Remove = function (OBJ ){
VaR Index = This. indexof (OBJ );
If (index> = 0 ){
This. removeat (INDEX );

Usage:Copy codeThe Code is as follows: var A = [];
For (VAR I = 0; I <5; I ++) A. insertat (I, I );
Alert ();
A. removeat (1 );
Alert ();

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.