[Code Sea pick up the shell JS] JS to delete elements from an array

Source: Internet
Author: User
Tags javascript array

Preface

To remove an element from a list in Java, you can use the Remove method directly.

In the JS array does not have the Remove method, but in the JS array has the Splice method can achieve the same effect, in addition, you can also use other ways to achieve this effect.


To remove an element from an array using the Splice method

First look at how the Splice method is used.

Grammar

Arrayobject.splice (index,howmany,item1,....., ItemX)
Parameters Description
Index Necessary. An integer that specifies the location of the Add/remove item, using a negative number to specify the position from the end of the array.
Howmany Necessary. The number of items to delete. If set to 0, the item is not deleted.
Item1, ..., ItemX Optional. Adds a new item to the array.


It is important to note that this method changes the original array.

The simplest example:--Delete an element of an array

<script>var array1 = ["Name1", "name2", "Name3", "Name4"];array1.splice (All); alert (array1);</script>

Output is: name1,name3,name4

Analytical:

1. Splice-delete the second element.

The first 1 is the position, position starting from 0, this is not difficult to understand;

The second 1 is the number that deletes an element.

2. Array1 = XXX is not required; The value of the array is changed. The reason is also said above.


Continue, if you want to add an element at the same time as the deleted location:

<script>var array1 = ["Name1", "name2", "Name3", "Name4"];array1.splice (the "Name5"); alert (array1); </script >

Output: Name1name5,,name3,name4



Delete elements in a complex array

The elements in the above example array are generic strings.

Here the so-called complex array is an element in an exponential group that is an object rather than a simple string.

Before starting this, write a method to turn JS array into str:

function arraytostring (array) {var str = ""; if (array!=null&&array.length>0)    {    str + = "["; for (Var i= 0;i<array.length;i++)    {   var objstr = "";   Objstr + = "{";   var obj = array[i];   for (var key in obj)   {        Objstr + = Key;objstr + = ": '"; objstr + = Obj[key];objstr + "'"; objstr + = ",";   }   if (objstr.length>3)   {  objstr = objstr.substring (0,objstr.length-1);   }    Objstr + = "}";   str + = OBJSTR;   if (i<array.length-1)   {  str + = ",";   }} str + = "]";} return str;}

Next, delete an array element with an element type of object

<script>var obj1 = {key: ' Key1 ', Name: ' name1_1 '};var obj2 = {key: ' Key2 ', Name: ' name1_1 '};var obj3 = {key: ' Key3 ', name : ' name2_2 '};var obj4 = {key: ' Key4 ', Name: ' name2_2 '};var array1 = [Obj1,obj2,obj3,obj4];//delete by Keyvar delkey = ' Key2 '; for (Var i=0;i<array1.length;i++) {var keytemp = array1[i].key;if (Keytemp===delkey)    {array1.splice (i,1);}} Alert (arraytostring (array1));</script>

Description

1. The key here is unique, the name value is not

2. Here the array loop is not using foreach because IE does not support

Details see:

3. The above is the element that removes the unique key value.

The Javascript array loops through the foreach


In general, only one element is deleted at a time, and if multiple elements are deleted at a time?

Similar to delete the name value is the "name1_1" element, corresponding to the two elements, the wording corresponds to should be:

<script>var obj1 = {key: ' Key1 ', Name: ' name2_2 '};var obj2 = {key: ' Key2 ', Name: ' name2_2 '};var obj3 = {key: ' Key3 ', name : ' name1_1 '};var obj4 = {key: ' Key4 ', Name: ' name1_1 '};var array1 = [Obj1,obj2,obj3,obj4];//delete by Namevar Delname= ' Name2_2 "; for (var i=0;i<array1.length;i++) {var nametemp = array1[i].name;if (nametemp===delname)    { Array1.splice (i,1);}} Alert (arraytostring (array1));</script>

But be careful, the above notation is wrong.

After the first deletion of an element, the length of the array1 changes, and the elements corresponding to each position change.

So the above wording:

1. Either an error occurred and no element was found for the specified location

2. The result of either execution is incorrect. The above example belongs to this.


In response to this situation, the solutions that can be thought of are:

1. Delete one at a time, multiple loops several times

2. Another way to do this is to add a new array to the array that does not need to be deleted, and then replace the old array with the array.


[Code Sea pick up the shell JS] JS to delete elements from an array

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.