ArraySplice

Description

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.

Returns

Updated array.

Syntax

ArraySplice(array,index[,elementCountForRemoval,replacements])

Member function

array.splice(index,elementCountForRemoval,replacements)

History

New in ColdFusion (2018 release) Update 5: Added the function.

Parameters

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.

Examples

Example 1

<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>

Output

Splice

Example 2

<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>

 

Output

Array splice

Example 3

<cfscript> 
 months = ['Jan', 'March', 'April', 'June'] 
 item=["Feb"] 
 // Insert at position -3 while removing 0 elements 
 ArraySplice(months,-3, 1, item) 
 WriteDump(months) 
</cfscript>

 

Output

Array splice

Example 4

<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>

Output

splice5

Member function

<cfscript> 
    months = ['Jan', 'March', 'April', 'June'] 
    item=["Feb"] 
    // Insert at position 3 while removing 2 elements 
    months.Splice(3, 2, item) 
    WriteDump(months) 
</cfscript>

 Adobe

Get help faster and easier

New user?