JavaScript splice

 The JavaScript splice function can be used to modify the content of the JavaScript Array array . By setting the array position and number of the splice and the parameter setting of the inserted element, you can add, modify or delete the array elements at will. JavaScript splice can set the order from the array. Several elements are processed, the number of array elements to be deleted, and which elements are to be added. You can also directly insert new elements to a certain position without deleting the elements. The usage of splice can be said to be quite flexible and free.


JavaScript splice basic syntax

array.splice( Index, quantity, inserted new element one, inserted new element two...)


The syntax can modify the array by adding splice directly after the array. The first parameter Index in parentheses represents the array element from which to delete or insert, which is the starting index value. The parameter value is a number and is Required item. The second parameter is "quantity", which means the number of new elements to be deleted or inserted. It is also a required item, but it can be 0, which means that no element will be deleted. After the third parameter All parameters are new elements to be added to the array, custom items.

JavaScript splice modify the array element example 1. Do not delete any elements and insert two new elements
<script type="text/javascript">
var NewArray = new Array(4);
NewArray[0] = "A";
NewArray[1] = "B";
NewArray[2] = "C";
NewArray[3] = "D";
document.write('Before modification:'+NewArray+'<br>');
NewArray.splice(1,0,"X","Y");
document.write('After modification:'+ NewArray+'<br>');
</script>
The output result is as
Before modification: A, B, C, D
After modification: A, X, Y, B, C, D

The example first declares a new array, NewArray, which has 4 array values. We need to use the splice function to insert the new elements of X and Y from the second position of the array without deleting any original Array elements of, so X, Y are inserted between the output results A and B. In the example, the first parameter Index of splice is set to 1, which actually means to start processing from the second element. If you want to start from the first element, Index must be set to 0. We set the second parameter to 0, which means not to delete any original parameters, just insert X and Y directly into the second position in the array.

JavaScript splice modify the array element example two, delete two elements and insert two new elements
<script type="text/javascript">
var NewArray = new Array(4);
NewArray[0] = "A";
NewArray[1] = "B";
NewArray[2] = "C";
NewArray[3] = "D";
document.write('Before modification:'+NewArray+'<br>');
NewArray.splice(1,2,"X","Y");
document.write('After modification:'+ NewArray+'<br>');
</script>
The output result is as
Before modification: A, B, C, D
After modification: A, X, Y, D

The biggest difference between this example and the first example is that the second parameter of splice is set to 2, which means that splice will delete two elements from the second element, and insert X, Y into that position, so the output is You can see that B and C have been deleted and changed to X and Y. This is the main function of the JavaScript splice function.

Post a Comment

0 Comments