There are many ways to process arrays. splice () is the most powerful. It can be used to insert, delete, or replace array elements. Here is a one-to-one introduction! 1. Delete-used to delete an element. There are two parameters: the first parameter (the location of the first item to be deleted) and the second parameter (the item to be deleted ).
There are many ways to process arrays. splice () is the most powerful. It can be used to insert, delete, or replace array elements. Here is a one-to-one introduction!
1. Delete-used to delete an element. There are two parameters: the first parameter (the location of the first item to be deleted) and the second parameter (number of items to be deleted)
2. Insert-insert any element to the specified position of the array. Three parameters: the first parameter (actual location), the second parameter (0), and the third parameter (inserted item)
3. replace-insert any element to the specified position of the array, and delete any number of items, three parameters. The first parameter (start position), the second parameter (number of deleted items), and the third parameter (insert any number of items)
You can see the following code!
Var lang = ["php", "java", "javascript"]; // Delete var removed = lang. splice (); alert (lang); // php, javascriptalert (removed); // java, return the deleted item // insert var insert = lang. splice (0th, "asp"); // insert alert (insert) from the first position; // return an empty array alert (lang); // asp, php, javascript // replace var replace = lang. splice (, "c #", "ruby"); // delete an item and insert two alert (lang); // asp, c #, rubyalert (replace ); // php, return the deleted item