Copy link to clipboard
Copied
Adobe ColdFusion (2021 release) includes a few new array functions. In this post, I shall list (pun unintended!) the new functions and also include the CFFiddle links so that you can also try out the functions before productionizing them.
ArrayPush
Adds an element or an object to the end of an array. For more information, see ArrayPush.
Syntax
ArrayPush(array,value)
Example 1
<cfscript>
arr=[23,65,187,81,9]
ArrayPush(array=arr,value=17)
WriteOutput("The size of the array is now: " & arrayLen(arr))
</cfscript>
Example 2
<cfscript>
arr=[{"id":101,"name":"John"},
{"id":102,"name":"Paul"},
{"id":103,"name":"George"}
]
ArrayPush(arr,{"id":104,"name":"Ringo"})
WriteOutput("The size of the array is now: " & arrayLen(arr))
</cfscript>
ArrayPop
Removes the last element from an array. For more information, see ArrayPop.
Syntax
ArrayPop(array)
Example
<cfscript>
arr=[{"id":101,"name":"John"},
{"id":102,"name":"Paul"},
{"id":103,"name":"George"}
]
// Array push
ArrayPush(arr,{"id":104,"name":"Ringo"})
// Array pop
WriteDump(ArrayPop(arr))
writeDump(arr)
</cfscript>
ArrayShift
Removes the first element of an array and returns the element that is removed. For more information, see ArrayShift.
Syntax
ArrayShift(array)
Example
<cfscript>
arr=[{"id":101,"name":"John"},
{"id":102,"name":"Paul"},
{"id":103,"name":"George"}
]
shifted=ArrayShift(arr)
WriteDump(shifted)
</cfscript>
ArrayUnshift
Adds one or more elements to the beginning of an array and returns the new length of the array. For more information, see ArrayUnshift.
Syntax
ArrayUnshift(array,object)
Example 1
<cfscript>
arr=["Mar","Apr","May","Jun","Jul"]
// List as first element
unshifted=ArrayUnshift(arr,"Jan,Feb") // Returns 6
WriteOutput(unshifted)
</cfscript>
Example 2
<cfscript>
arr=["Mar","Apr","May","Jun","Jul"]
// Array as first element
unshifted=ArrayUnshift(arr,["Jan,Feb"])
WriteOutput(unshifted) // Returns 6
</cfscript>
ArrayReduceRight
Iterates over every entry of the array and calls the closure to work on the elements of the array. For more information, see ArrayReduceRight.
Syntax
ArrayReduceRight(array, function(result, item, [,index, array])[, initialValue])
Example 1
<cfscript>
data = ['1','2','3','4','5','6'];
stringConcat = ArrayReduceRight(data,function(previous,next) {
return previous & next;
},"");
writeOutput(stringConcat)
</cfscript>
Example 2
<cfscript>
data=[3,5,7,9,11]
result=ArrayReduceRight(data,function(previous,next){
return previous & next
},"")
writeDump(result)
</cfscript>
Copy link to clipboard
Copied
Hi, Saurav! How is ArrayPush different from ArrayAppend? I know we all like to have "push" and "pop" functionality with arrays in general, but it seems like we have two names for the same kind of function now.
Dave Watts, Eidolon LLC
Copy link to clipboard
Copied
Dave Watts : I know we all like to have "push" and "pop"
Funny you should say that, Dave. I think it's because there is a familiar swing to the pushin' and the poppin'.
Copy link to clipboard
Copied
There is, in any case, one difference between arrayAppend and arrayPush. The one returns a boolean (whether or not the item was successfully appended), the other, an integer (the number of items in the resulting array).