Skip to main content
Community Manager
December 22, 2020
Question

New array functions in ColdFusion (2021 release)

  • December 22, 2020
  • 1 reply
  • 828 views

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>

Try in CFFiddle

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>

Try in CFFiddle

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>

Try in CFFiddle

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>

Try in CFFiddle

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>

Try in CFFiddle

Example 2

<cfscript>
    arr=["Mar","Apr","May","Jun","Jul"]
    // Array as first element
    unshifted=ArrayUnshift(arr,["Jan,Feb"])
    WriteOutput(unshifted) // Returns 6
</cfscript>

Try in CFFiddle

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>

Try in CFFiddle

Example 2

<cfscript>
    data=[3,5,7,9,11]
    result=ArrayReduceRight(data,function(previous,next){
        return previous & next
    },"")
    writeDump(result)
</cfscript>

Try in CFFiddle

This topic has been closed for replies.

1 reply

Community Expert
December 22, 2020

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

Dave Watts, Eidolon LLC
BKBK
Community Expert
Community Expert
December 23, 2020

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

BKBK
Community Expert
Community Expert
December 26, 2020

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