Tag: not func Specifies the usage language-the specified position return while
In the beginning of the sophomore contact with JavaScript, there is a problem in the learning function:
A function that defines a 2 parameter. The 1th parameter is an array, and the 2nd parameter is the element that needs to be deleted. function function, find the value provided by the 2nd argument in the 1th real parameter group, find the element to delete (a few deletions), if the deleted element is not the last element, you need to move the back element forward in turn. The output deletes all elements in the front and back parameter group and the real parameter group. Real parameter Group "3,78,34,123,4,5,66,34,55", delete 34.
Find and delete in the specified array, a bit similar to the pointer in C language.
1<! DOCTYPE html>234 5<title> Testing </title>6<script>7 functionDelarr (arr,x) {8 varI=arr.length;9 while(i--){Ten if(arr[i]==x) { OneArr.splice (i,1); A returnarr; - } - } the } - vararr=[3,78,34,123,4,5,66,34,55]; - alert (arr); -Alert (Delarr (arr,34)); + - +</script> A at -<body> - -</body> - -JS Splice ()
Definition and usageThe splice () method adds/deletes an item to/from the array, and then returns the item that was deleted.
Note: This method changes the original array.
There are 3 ways in which the splice () method works in javascript:
Delete--You can delete any number of items, just specify 2 parameters: the location of the first item to delete and the number of items to delete.
For example, splice (0,2) deletes the first two items in the array.
Insert--You can insert any number of items to the specified location, providing only 3 parameters: Insert starting position, 0 (number of items to delete), and items to insert. If you want to insert more than one item, you can pass in four, five, any number of items.
For example, splice (2,0, "Red", "green") will insert the string "red" and "green" starting at position 2.
Replace-A comprehensive application that deletes and inserts the number of equal items, you can insert any number of items at the specified location, and delete any number of items at the same time by specifying 3 specified parameters: The starting position, the number of items to delete, and any number of items to insert. The number of items inserted is not necessarily equal to the number of items deleted.
Hints and NotesNote: Notice that the splice () method works differently than the slice () method, and the splice () method modifies the array directly.
Example:
1<script>2 varcolors = ["Red", "green", "blue"];3 varremoved = Colors.splice (0,1);//Delete First item4alert (colors);//Green,blue5alert (removed);//red, the value in the returned array contains an item6 7removed = Colors.splice (1, 0, "yellow", "orange");//insert two items starting from position 18alert (colors);//Green,yellow,organge,blue9alert (removed);//an empty array is returned.Ten Oneremoved = Colors.splice (1, 1, "Red", "purple");//insert two items to delete an item Aalert (colors);//Green,red,purple,orange,blue -alert (remove);//Yellow, the returned array contains only one item -</script>
Reference Source: http://www.cnblogs.com/pumushan/p/5507862.html
The use of function splice () in JavaScript.