A quick post on the new function arraySlice added to the list of functions in ColdFusion 10. The arraySlice function is used to select a part of the array. It takes three arguments – array, offset and length.
arraySlcie(array,offset,[length])
The offset argument specifies the position from which array has to sliced. It is also possible to specify a negative value for the offset argument. When a negative value is specified, the sequence would start from the end of the array (see example). The third argument – length is an optional argument, when specified the new array will contain that many elements and when not specified, part of the array starting from the offset position is selected.
One thing to note here is that, irrespective of the value specified for the offset (positive or negative), elements are selected by incrementing the index value. As observed in the above code snippet, when a negative offset value is specified(-5) the sequence would start from the end of the array and select elements from left to right(4, 5, 6).
arraySlcie(array,offset,[length])
The offset argument specifies the position from which array has to sliced. It is also possible to specify a negative value for the offset argument. When a negative value is specified, the sequence would start from the end of the array (see example). The third argument – length is an optional argument, when specified the new array will contain that many elements and when not specified, part of the array starting from the offset position is selected.
<cfscript>
array = [1, 2, 3, 4, 5, 6, 7, 8];
newArray = arraySlice(array, 2, 3); //returns 2, 3, 4
newArray = arraySlice(array, 4); //returns 4, 5, 6, 7, 8
newArray = arraySlice(array, -5, 3); //returns 4, 5, 6
</cfscript>
One thing to note here is that, irrespective of the value specified for the offset (positive or negative), elements are selected by incrementing the index value. As observed in the above code snippet, when a negative offset value is specified(-5) the sequence would start from the end of the array and select elements from left to right(4, 5, 6).
Comments
Post a Comment