arraySplice

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]) → returns array

Member Function Syntax

someArray.splice(index[, elementCountForRemoval, replacements])

This function requires Adobe ColdFusion 2018.0.5 and up.  Not supported on Lucee, etc.

Argument Reference

array array
Required

The array to splice and modify.

index numeric
Required

The position at which to start modifying the array. If the position is greater than the length of the array, the replacement elements will be inserted after the last array element. If the position is less than 0, the start is set to the end of the array and the origin is set accordingly.

elementCountForRemoval numeric

The number of elements to be removed starting with the start index.

replacements array

Array of elements to be added to the array starting with index start.

Examples
Sample code invoking the arraySplice function

months = ['Jan', 'March', 'April', 'June'];
item = ["Feb"];
arraySplice( months, 2, 0, item );
writedump(months);

Expected Result: ["Jan","Feb","March","April","June"]

months = ['Jan', 'March', 'April', 'June'];
item = ["Feb"];
arraySplice( months, 3, 2, item );
writedump(months);

Expected Result: ["Jan","March","Feb"]

months = ['Jan', 'March', 'April', 'June'];
item = ["Feb"];
arraySplice( months, -3, 0, item );
writedump(months);

Expected Result: ["Jan","Feb","March","April","June"]

months = ['Jan', 'March', 'April', 'June'];
item = ["Feb"];
arraySplice( months, 5, 0, item );
writedump(months);

Expected Result: ["Jan","March","April","June","Feb"]

months = ['Jan', 'March', 'April', 'June'];
item = ["Feb"];
months.splice(  2, 0, item );
writedump(months);

Expected Result: ["Jan","Feb","March","April","June"]

Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.

Fork me on GitHub