JS Delete Array function

Source: Internet
Author: User

Reference Address: http://www.398588.com/Article/T8/96.aspx

It is well known that everyone should know the use of Delete, to delete the object or delete the array, etc., if the object is deleted outside the divisor may be deleted, "clean" is what it means that in the JS array

, if delete is used to remove an array that might be currently deleted, instead of removing it, assign the value to null or NULL, as shown in the following code:
var array = ["A", "B", "C", "D", "E"];
Delete Array[2]; C was removed.
alert (array.length); Pop it up and see what it shows? The answer is yes, it should be 5.
There will be a problem, delete A should have left the next 4 to do, why I do not know, if you want to completely delete one of the array, look at the following code:
var array = ["A", "B", "C", "D", "E"]; Also declare a set of arrays and set the initial value for him.
Array.splice (2,1); Note here that the most important code is here.
alert (array.length); Pop-up shows, haha c successfully deleted.
Alert (Array.join (",")); To see more clearly, we use the Join function to merge the array for viewing, and the result is: A,b,d,e.
Above just delete already know an array, if there is an array is empty, I want to delete the empty arrays, then this method can not be used? Let's try the following code first:

var array = ["A", "" "," C "," "," E "];
For (k in array)
if (array[k] = = "") Array.splice (k,k+1); Note here that the second parameter is not 1, but k+1.


Splice's instructions in Jscript.chm are:
Removes one or more elements from an array and, if necessary, inserts a new element at the location of the removed element, returning the element that was removed.
Parameters
Arrayobj

Required option. An Array object.

Start

Required option. Specifies the starting position of the element to be removed from the array, which is calculated starting at 0.

DeleteCount

Required option. The number of elements to remove.

Item1, item2,.. ., itemn

Required option. The new element to insert at the location of the removed element.

Description
The splice method modifies the Arrayobj by removing the specified number of elements from the start position and inserting the new element. The return value is a new Array object that consists of the elements that were removed.

JS Delete Array function

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.