Js methods for deleting arrays

  • 2020-03-30 02:04:07
  • OfStack

Var arr = [' a ', 'b', 'c'].

There are two ways to delete 'b' :

1. Delete method :delete arr[1]

The arr[1] is undefined, but it has the advantage that the index of the original array remains the same

for(index in arr)

This traversal skips the undefined elements in it

2. Array object splice method :arr.splice(1,1);

In this way the array length changes, but the original array index also changes

The first 1 in the splice parameter is the removed starting index (from 0), which is the second element of the array

The second one is the number of elements that are deleted.

Traversing group elements can be done in a normal way, such as for, because the deleted elements are not retained in the array

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

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

The resulting array becomes arr:'a','d','e','c'

JavaScript truncating an array by setting its length property is the only way to shorten the length of an array.


 //And what we often do is we refactor an array by walking through it.

  if(isNaN(dx)||dx>this.length){return false;}
  for(var i=0,n=0;i<this.length;i++)
 a = ['1','2','3','4','5'];
 alert("elements: "+a+"nLength: "+a.length);
 a.remove(0); //Delete elements with subscript 0
 alert("elements: "+a+"nLength: "+a.length);

 //We can also do this with splice.
 Array.prototype.baoremove = function(dx)
  if(isNaN(dx)||dx>this.length){return false;}
 b = ['1','2','3','4','5'];
 alert("elements: "+b+"nLength: "+b.length);
 b.baoremove(1); //Delete elements subscript 1
 alert("elements: "+b+"nLength: "+b.length);

We know that in IE5 or later, JavaScript's Array objects do not provide an off-the-shelf way to delete Array elements. In IE5.5+, there is a splice method, but instead of deleting an item (or items), it simply clears the value of an item (or items), meaning that the item still exists and the length of the array has not changed.

In fact, we can add a delete method to the array ourselves (note that this means actually removing an item from the array). You might think of using loops to reassign arrays, which is fine, but inefficient.

Next we introduce two methods, slice and concat, which use the Array object to customize the method of deleting an Array.

Note the comments in the code below.

Array.prototype.del=function(n) { //N is the number of terms, starting at 0.
//Prototype is an object prototype. Notice how to add custom methods to objects.
 if(n<0) //If n<0, nothing is done.
  return this;
  return this.slice(0,n).concat(this.slice(n+1,this.length));
   concat Method: returns a new array composed of two or more arrays. 
 So here is the return this.slice(0,n)/this.slice(n+1,this.length)
    The new array that's formed, in the middle, is just missing the number one n Items. 
   slice Methods:   Returns a segment of an array with two arguments, specifying the starting and ending positions. 
//Let's try this do-it-yourself method
var test=new Array(0,1,2,3,4,5);
test=test.del(3); //So if I start at 0, I'm going to delete the fourth term.

In this way, only two methods of the Array object are used flexibly to achieve our requirements.

Related articles: