Write in front
In recent projects, useful to the JS array of operations, before you almost no use of this method, here to record, is a summary of what is learned.
Array Object Splice method
The splice () method adds/deletes an item to/from the array, and then returns the item that was deleted. The method changes the original array.
Basic syntax
Arrayobject.splice (index,howmany,item1,....., ItemX)
Parameter description
Index: Required. 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: Required. The number of items to delete. If set to 0, the item is not deleted.
Item1: Optional. Adds a new item to the array.
return value
Array: The new array after the element is deleted.
Description
The splice () method deletes 0 or more elements starting at index and replaces the deleted elements with one or more values declared in the argument list.
If the element is removed from the Arrayobject, the array containing the deleted element is returned.
Array Splice Method The first argument is the starting position, and the second parameter is the number to delete.
<script>
var arr = [1, 2, ' A ', ' B '];
Console.log (' original array ');
Console.log (Arr.join (', '));
Arr.splice (2, 1);
Console.log (' array after deletion of elements ');
Console.log (Arr.join (', '));
</script>
Results
Arr.splice (2,1): Delete the element starting with index=2, 1: The number of deleted elements is 1. If you want to delete the elements after 2 can be written like this: Arr.splice (2,2);
<script> var arr = [1, 2, ' A ', ' B ']; Console.log (' original array '); Console.log (Arr.join (', ')); Arr.splice (2, 1, ' Wolfy '); Console.log (' array after deletion of elements '); Console.log (Arr.join (', ')); </script>
In this example, we delete the element with index 2 and add the new element ' Wolfy '. Somewhat similar to replacement.
Summarize
Removes array-specific elements from the project, or is more commonly used, and is good at using some native methods of array.
[Js] To delete an array of specified elements