Solution to the deletion of JavaScript array elements (1)

Source: Internet
Author: User
Tags javascript array

Are you familiar with the methods for deleting JavaScript array elements? I would like to share with you here. I hope to learn how to delete JavaScript array elements through this article.

Deletion of JavaScript array elements

Vararr = ['A', 'B', 'C'];

To delete 'B', you can use either of the following methods:

1. delete method: deletearr [1]

In this way, the length of the JavaScript array remains unchanged. At this time, the arr [1] is changed to undefined, but the index of the original JavaScript array remains unchanged. In this case, you need to traverse the JavaScript array element before using it.

For (indexinarr)

Document. write ('Arr ['+ index +'] = '+ arr [index]);

This Traversal method skips the undefined element.

◆ This method will be supported by IE4.o later

2. Method of JavaScript Array object splice: arr. splice );

In this way, the length of the JavaScript array is changed, but the index of the original JavaScript array is also changed.

The first 1 in the splice parameter is the initial index to be deleted (from 0), which is the second element of the JavaScript array.

The second one is the number of elements deleted. Only one element is deleted here, that is, 'B ';

In this case, the elements in the JavaScript array can be traversed in the normal way, for example, because the deleted elements are

The JavaScript array is not retained.

◆ This method is supported only after IE5.5

It is worth mentioning that the splice method can add JavaScript array elements while deleting JavaScript array elements.

For example, arr. splice (, 'D', 'E'), d, and e are added to the JavaScript array arr.

Result: The JavaScript array is changed to arr: 'A', 'D', 'E', and 'c'

<Big> external article: </big>

JavaScript truncates JavaScript arrays by setting the length attribute of JavaScript arrays. This is the only way to shorten the length of JavaScript arrays. if you use the delete operator to delete elements in the JavaScript array, although the element becomes undefined, The length attribute of the JavaScript array does not change the two ways to delete elements, and the length of the JavaScript array also changes.

 
 
  1. /*
  2. * Method: Array. remove (dx)
  3. * Function: delete JavaScript array elements.
  4. * Parameter: dx deletes the subscript of an element.
  5. * Return value: Modify the JavaScript array on the original JavaScript array.
  6. */
  7. // It is often used to reconstruct the JavaScript Array Through traversal.
  8. Array. prototype. remove = function (dx)
  9. {
  10. If (isNaN (dx) | dx> this. length) {returnfalse ;}
  11. For (vari = 0, n = 0; I <this. length; I ++)
  12. {
  13. If (this [I]! = This [dx])
  14. {
  15. This [n ++] = this [I]
  16. }
  17. }
  18. This. length-= 1
  19. }
  20. A = ['1', '2', '3', '4', '5'];
  21. Alert ("elements:" + a + "nLength:" + a. length );
  22. A. remove (0); // Delete the element whose subscript is 0
  23. Alert ("elements:" + a + "nLength:" + a. length );
  24. /*
  25. * Method: Array. baoremove (dx)
  26. * Function: delete JavaScript array elements.
  27. * Parameter: dx deletes the subscript of an element.
  28. * Return value: Modify the JavaScript array on the original JavaScript array.
  29. */
  30. // We can also use splice.
  31. Array. prototype. baoremove = function (dx)
  32. {
  33. If (isNaN (dx) | dx> this. length) {returnfalse ;}
  34. This. splice (dx, 1 );
  35. }
  36. B = ['1', '2', '3', '4', '5'];
  37. Alert ("elements:" + B + "nLength:" + B. length );
  38. B. baoremove (1); // Delete the element whose subscript is 1.
  39. Alert ("elements:" + B + "nLength:" + B. length );
  40.  

We know that in IE5 or earlier versions, the JavaScript ArrayJavaScript array) object does not provide a ready-made method to delete JavaScript array elements. In IE5.5 + versions, although there is a splice method, it does not delete one or more items), but only clears the value of one or more items, that is to say, this item still exists, and the length of the JavaScript array has not changed.

In fact, we can add a deletion method for the JavaScript array by ourselves. Note that this refers to removing a part of the JavaScript array from the JavaScript array member ). You may want to use loops to re-assign values to JavaScript arrays. This is certainly possible, but it is inefficient.


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.