Modifies an array by removing elements and adding new elements. It starts from the index, removes as many elements as specified by elementCountForRemoval, and puts the replacements starting from index position.
ArraySplice(array,index[,elementCountForRemoval,replacements])
array.splice(index,elementCountForRemoval,replacements)
Parameter |
Required/Optional |
Description |
array |
Required |
The array to splice and modify. |
index |
Required |
The position at which to start modifying the array. If the position is greater than the length of the array, the start is set to the length of the array. If the position is less than 0, the start is set to the beginning of the array and the origin is set accordingly. |
elementCountForRemoval |
Optional |
The number of elements to be removed starting with the start index. |
replacements |
Optional |
Array of elements to be added to the array starting with index start. |
<cfscript> months = ['Jan', 'March', 'April', 'June'] item=["Feb"] // Insert Feb at position 2 while removing 0 elements ArraySplice(months,2, 0, item) WriteDump(months) </cfscript>
<cfscript> months = ['Jan', 'March', 'April', 'June'] item=["Feb"] // Insert at position 3 while removing 2 elements ArraySplice(array=months,index=3, elementCountForRemoval=2, replacements=item) WriteDump(months) </cfscript>
<cfscript> months = ['Jan', 'March', 'April', 'June'] item=["Feb"] // Insert at position -3 while removing 0 elements ArraySplice(months,-3, 1, item) WriteDump(months) </cfscript>
<cfscript> months = ['Jan', 'March', 'April', 'June'] item=["Feb"] // Insert at position 5, which is greater that the length of array ArraySplice(months,5, 0, item) WriteDump(months) </cfscript>
<cfscript> months = ['Jan', 'March', 'April', 'June'] item=["Feb"] // Insert at position 3 while removing 2 elements months.Splice(3, 2, item) WriteDump(months) </cfscript>